是否有任何可能的方法从BroadcastReceiver生成通知

时间:2015-02-27 07:49:28

标签: android

我需要从广播接收器生成一个titlbar通知。 要么 带有通知声音的警报对话

2 个答案:

答案 0 :(得分:0)

是的,你可以,这是我最新项目的一个例子:

public class AlarmReceiver extends BroadcastReceiver {


private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;


@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

    Intent myIntent = new Intent();

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, MY_NOTIFICATION_ID,
            myIntent, PendingIntent.FLAG_ONE_SHOT);



    myNotification = new NotificationCompat.Builder(context)
            .setContentTitle("Game Time!")
            .setContentText("Where Amazing happens")
            .setTicker("Hello !")
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setSmallIcon(R.drawable.ic_launcher)
            .build();

    notificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

}

您还可以从BroadcastReceiver中调用另一个活动。

希望这有帮助!

答案 1 :(得分:-1)

可以在活动或服务中生成通知。修改您的广播接收器并使用其Context来生成它。最好发布你的代码:)