是否可以让通知启动广播接收器?
我试过这段代码,但它不起作用。
通知已创建,但是当我单击它时,什么也没有发生。
注意:当我将 notificationIntent 更改为从 MyBroadcastReceiver.class 指向一个活动(如 MainActivity.class)时,它工作正常。
通知创建:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
int notificationIconId = XXXXXX
Notification notification = new Notification(
notificationIconId,
XXXXXX,
System.currentTimeMillis()
);
CharSequence contentTitle = XXXXXXX
CharSequence contentText = XXXXXX
Intent notificationIntent = new Intent(context,MyBroadcastReceiver.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(1,notification);
这是广播接收器
public static class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*
*/
}
}
AndroidManifest.xml 内部
<receiver android:name=".MyBroadcastReceiver" />