在我的IntentService類中,我創建了通知並分配ID = 1213,並且一旦應用程序打開,通知就會顯示出來。Android我的BroadcastReceiver類沒有收到通知發送的動作
Intent cancelScan = new Intent();
cancelScan.setAction(CANCEL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1213, cancelScan, PendingIntent.FLAG_UPDATE_CURRENT);
mNbuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel,"Cancel Scanning",pendingIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, mNbuilder.build());
在我的廣播接收器類
if(CANCEL.equals(intent.getAction())){
Log.i(TAG,"Received Broadcasr Receiver");
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//context.stopService(new Intent(context,ScanService.class));
nm.cancel(NOTIFICATION_ID);
}
}
和我的manifest.xml
<receiver android:name=".WifiScanReceiver" android:enabled="true">
<intent-filter>
<action android:name="CANCEL"/>
</intent-filter>
</receiver>
我試過幾次,當我點擊下方的通知的動作按鈕,但沒有logcat中不打印任何東西。我做錯了哪些部分?預先感謝。
這行中'CANCEL'的值是什麼:'cancelScan.setAction(CANCEL)' – 0xDEADC0DE
private final String CANCEL =「CANCEL」,變量和值全部使用相同的名字。感謝您的回覆:) – noleavename
您是否確定您的'BroadcastReceiver'中的'CANCEL'的值是否相同? – 0xDEADC0DE