我正在使用自定义视图创建通知,其中包含两个按钮(是/否)。单击这些按钮将启动PendingIntent,然后根据单击的按钮继续。 但是PendingIntent.getBroadcast()总是返回第一个Intent(因此PendingIntent调用的BroadcastReceiver总是认为第一个声明的按钮"没有"被点击),这可能是由于缺少Flags。
如何解决这个问题?我需要另一个BroadcastReceiver吗?
// create notification with custom view and two buttons
Intent buttonNoReceiver = new Intent(context, NotificationAfterReceiver.class);
buttonNoReceiver.putExtra(AppUtil.EXTRAS_EVENT_ID, event.getEventID());
buttonNoReceiver.putExtra(EXTRAS_BUTTON, "no");
PendingIntent pendingNoIntent = PendingIntent.getBroadcast(context, event.getEventID(), buttonNoReceiver, 0);
views.setOnClickPendingIntent(R.id.btn_no, pendingNoIntent);
Intent buttonYesReceiver = new Intent(context, NotificationAfterReceiver.class);
buttonYesReceiver.putExtra(AppUtil.EXTRAS_EVENT_ID, event.getEventID());
buttonYesReceiver.putExtra(EXTRAS_BUTTON, "yes");
PendingIntent pendingYesIntent = PendingIntent.getBroadcast(context, event.getEventID(), buttonYesReceiver, 0);
views.setOnClickPendingIntent(R.id.btn_yes, pendingYesIntent);
NotificationAfterReceiver
public class NotificationAfterReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String button = null;
Bundle extras = intent.getExtras();
if (extras != null) {
button = extras.getString(NotificationUtil.EXTRAS_BUTTON);
}
if (button.equals("yes")) {
values.put(DatabaseHelper.KEY_EVENTS_FULFILLED, 1);
Log.i("NotificationAfterReceiver", "button yes");
} else if (button.equals("no")) {
values.put(DatabaseHelper.KEY_EVENTS_FULFILLED, 0);
Log.i("NotificationAfterReceiver", "button no");
}
}
}
答案 0 :(得分:0)
只是为了添加一个正确的答案,使PendingIntent
唯一的可能解决方案是使用当前时间,如:
PendingIntent uniquePendingIntent =
PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), intent, 0);