創建通知:Android的通知的addAction不啓動廣播
PendingIntent pIntent = PendingIntent.getActivity(context, (int) taskId, intent, 0); intent.setAction(Utils.MARK_AS_DONE); PendingIntent pIntentMarkAsDone = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setTicker(ticker) .setContentTitle(title) .setContentText(description) .setSmallIcon(getAlarmIcon(type)) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher)) .setContentIntent(pIntent) .addAction(0, context.getString(R.string.mark_as_done), pIntentMarkAsDone); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify((int) taskId, notification);
我添加使用帶getBroadcast掛起的意圖增加。
接收機:
public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Log to check } }
此類應 「接收」 的動作。我還加上清單
清單:
<receiver android:name=".NotificationReceiver"> <intent-filter> <action android:name="<package_name>.MARK_AS_DONE"/> </intent-filter> </receiver>
好,的onReceive不接收。我究竟做錯了什麼?
解決!謝謝! 問題確實如此:new Intent(context,NotificationReceiver.class) –