确定addAction点击Android通知
我正在尝试使用新的通知界面。 我已经在通知中添加了3个button,并且每次单击它们时都想将其保存到数据库中。
通知本身运作良好,被调用时显示,我只是不知道如何捕获三个不同的button点击每一个。
我正在使用BroadcastReceiver来捕捉点击,但我不知道如何分辨哪个button被点击。 
 这是AddAction的代码(我已经排除了其他的通知,因为它的工作很好) – 
  //Yes intent Intent yesReceive = new Intent(); yesReceive.setAction(CUSTOM_INTENT); Bundle yesBundle = new Bundle(); yesBundle.putInt("userAnswer", 1);//This is the value I want to pass yesReceive.putExtras(yesBundle); PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes); //Maybe intent Intent maybeReceive = new Intent(); maybeReceive.setAction(CUSTOM_INTENT); Bundle maybeBundle = new Bundle(); maybeBundle.putInt("userAnswer", 3);//This is the value I want to pass maybeReceive.putExtras(maybeBundle); PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe); //No intent Intent noReceive = new Intent(); noReceive.setAction(CUSTOM_INTENT); Bundle noBundle = new Bundle(); noBundle.putInt("userAnswer", 2);//This is the value I want to pass noReceive.putExtras(noBundle); PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo); 
 这是BroadcastReceiver的代码 – 
  public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("shuffTest","I Arrived!!!!"); //Toast.makeText(context, "Alarm worked!!", Toast.LENGTH_LONG).show(); Bundle answerBundle = intent.getExtras(); int userAnswer = answerBundle.getInt("userAnswer"); if(userAnswer == 1) { Log.v("shuffTest","Pressed YES"); } else if(userAnswer == 2) { Log.v("shuffTest","Pressed NO"); } else if(userAnswer == 3) { Log.v("shuffTest","Pressed MAYBE"); } } } 
 我已经在清单中注册了BroadcastReceiver 。 另外,我想提一提的是,当点击通知中的某个button时, BroadcastReceiver被调用,但意图总是包含额外的'2'。 
 这是通知iteslf  –  
这是因为您正在使用具有相同操作的Intents的FLAG_UPDATE_CURRENT
从文档:
如果描述的PendingIntent已经存在,那么保留它,但是用这个新的Intent代替它的额外的数据。
 当您指定pendingIntentMaybe和pendingIntentNo ,系统使用为PendingIntent创build的PendingIntent ,但是它会覆盖额外的。 因此,所有三个variables都指向同一个对象,最后指定的是pendingIntentNo 。 
 您应该为每个Intent指定一个替代操作。 你仍然可以有一个BroadcastReceiver ,只要它拦截所有三个行动。 这将更less的语义混淆:) 
您的通知海报:
 //Yes intent Intent yesReceive = new Intent(); yesReceive.setAction(YES_ACTION); PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes); //Maybe intent Intent maybeReceive = new Intent(); maybeReceive.setAction(MAYBE_ACTION); PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe); //No intent Intent noReceive = new Intent(); noReceive.setAction(NO_ACTION); PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo); 
您的接收器:
 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(YES_ACTION.equals(action)) { Log.v("shuffTest","Pressed YES"); } else if(MAYBE_ACTION.equals(action)) { Log.v("shuffTest","Pressed NO"); } else if(NO_ACTION.equals(action)) { Log.v("shuffTest","Pressed MAYBE"); } } 
在我的情况下,它添加意图filter后为我工作
  <receiver android:name=".AlarmReceiver"> <intent-filter> <action android:name="YES_ACTION"/> <action android:name="NO_ACTION"/> <action android:name="MAYBE_ACTION"/> </intent-filter> </receiver> 
一步步
步骤1
 public void noto2() // paste in activity { Notification.Builder notif; NotificationManager nm; notif = new Notification.Builder(getApplicationContext()); notif.setSmallIcon(R.drawable.back_dialog); notif.setContentTitle(""); Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notif.setSound(path); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent yesReceive = new Intent(); yesReceive.setAction(AppConstant.YES_ACTION); PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); notif.addAction(R.drawable.back_dialog, "Yes", pendingIntentYes); Intent yesReceive2 = new Intent(); yesReceive2.setAction(AppConstant.STOP_ACTION); PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); notif.addAction(R.drawable.back_dialog, "No", pendingIntentYes2); nm.notify(10, notif.getNotification()); } 
步骤1.5
我创build了一个全局类AppContant
  public class AppConstant { public static final String YES_ACTION = "YES_ACTION"; public static final String STOP_ACTION = "STOP_ACTION"; } 
第2步:
  public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (AppConstant.YES_ACTION.equals(action)) { Toast.makeText(context, "YES CALLED", Toast.LENGTH_SHORT).show(); } else if (AppConstant.STOP_ACTION.equals(action)) { Toast.makeText(context, "STOP CALLED", Toast.LENGTH_SHORT).show(); } } 
}
第3步
  <receiver android:name=".NotificationReceiver"> <intent-filter> <action android:name="YES_ACTION"/> <action android:name="STOP_ACTION"/> </intent-filter> </receiver> 
 这里YES_ACTION必须是yourfullpackagename.YES 
喜欢
 private static final String YES_ACTION = "com.example.packagename.YES"; 
 同样你可以使用NO_ACTION或MAYBE_ACTION 
 在BroadcastReceiver中,您必须使用与YES_ACTION所声明的相同的YES_ACTION , 
意味着在BroadcastReceiver类中,您可以通过以下方式检查自定义广播
 public class NotificationReceiver extends BroadcastReceiver { private static final String YES_ACTION = "com.example.packagename.YES"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(YES_ACTION.equals(action)) { Toast.makeText(context, "CALLED", Toast.LENGTH_SHORT).show(); } } 
}
注意:而不是在YES_ACTIONstring中,您也可以使用其他字。