我现在尝试为年龄进行调试,我似乎无法找到问题:
我有一个广播接收器,它成功接收广播。 通知有两个操作("按钮"):
firstIntent = null;
secondIntent = null;
firstPendingIntent = null; //first "button" to join with the first intent
secondPendingIntent = null; //second "button" to join with the second intent
if(boolean){
//relevant
firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_that");
}else{
firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_that");
}
firstPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT);
secondPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, secondIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(notification_title)
.setContentText(notification_text)
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.addAction(R.drawable.abc_cab_background_top_holo_light, first_option, firstPendingIntent)
.addAction(R.drawable.abc_cab_background_top_holo_dark, second_option, secondPendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_name)
.build();
每当我在 broadcastReceiver 中进行调试时,出于某种原因,来自 extras 的操作始终会记录" do_that",即使我点击了第一个或第二个通知按钮。有什么理由吗?我似乎无法理解为什么。
public class NotificationFunctions extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean feedback;
String action = intent.getExtras().getString("action");
Log.wtf("...", action); //logs do_that
}}
答案 0 :(得分:0)
这有什么原因吗?
因为firstPendingIntent == secondPendingIntent
。
如果已经PendingIntent
符合您的请求,则getBroadcast()
会返回现有的PendingIntent
。 FLAG_UPDATE_CURRENT
表示要替换Intent
内包含的PendingIntent
中的额外内容。
在您的两个getBroadcast()
来电之一中,将0
替换为其他一些号码,以获取不同的PendingIntent
个对象。
此外,我建议您将getBaseContext()
和getApplicationContext()
替换为this
。只有当您知道正是为什么使用它们时才能使用getBaseContext()
和getApplicationContext()
。