0
두 개의 버튼 (예/아니오)이 포함 된 사용자 정의보기로 알림을 만듭니다. 이 버튼을 클릭하면 PendingIntent가 시작되고 클릭 한 버튼에 따라 진행됩니다. 그러나 PendingIntent.getBroadcast()는 항상 첫 번째 Intent를 반환합니다. 따라서 PendingIntent에 의해 호출되는 BroadcastReceiver는 누락 된 플래그로 인해 발생하는 첫 번째 선언 된 버튼 "no"를 항상 클릭한다고 생각합니다.PendingIntent.getBroadcast는 기존 PendingIntent를 반환합니다.
해결 방법? 다른 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);
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");
}
}
}
getBroadcast()를 호출 할 때 다른 'requestCode' 사용 – pskink
어떤 코드를 사용해야합니까? 기본 1은 1이라고 추측하지만 이것은 같은 문제를 일으킬 것입니다. 난수를 사용하는 것은 좋은 해결책이 아닌 것 같습니다 ..? – Chris
"아니오"PendingIntent에 0을 사용하고 "예"에 1을 사용합니다. PendingIntent – pskink