问题
I have a custom notification with three buttons ("ok", "cancel", "later") I can catch click event only when there is one button, but with the three buttons i can't know which button of them is clicked. My question is "How to know which button is clicked?"
// I use this code in My "createNotification" function to add event to buttons
Intent intent = new Intent(Context.NOTIFICATION_SERVICE);
PendingIntent pendinghomeIntent =
PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
notificationView.setOnClickPendingIntent(R.id.okButt, pendingrecentAppIntent);
notificationView.setOnClickPendingIntent(R.id.cancelButt, pendingrecentAppIntent);
notificationView.setOnClickPendingIntent(R.id.laterButt, pendingrecentAppIntent);
I craeted a service to register and unregister the broadcast receiver and this is done perfectly in case of one button. With the three buttons, when i click on each of them the same result (which is implemeneted in onReceive function) is happened, but i want for each of them a different result.
Please help me, thanks in advance
回答1:
You need to use separate PendingIntents
(made from Intents
with differecnt actions) for every button. Later in onReceive()
you just check action of incoming Intent
& execute different code depending on that.
回答2:
As Sam said, "You need to use separate PendingIntents(made from Intents with different actions) for every button." I found that i must use separate PendingIntents for every button but the concept of "different actions" not worked for me. I used the second parameter of setOnClickPendingIntent to distinguish between the three PendingIntents with use FLAG_UPDATE_CURRENT for the fourth parameter. My code is
Intent homeIntent = new Intent(Context.NOTIFICATION_SERVICE);
Bundle yesBundle = new Bundle();
yesBundle.putInt("userAnswer", 1);
homeIntent.putExtras(yesBundle);
PendingIntent pendinghomeIntent =
PendingIntent.getBroadcast(getBaseContext(), 0, homeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.homeButt, pendinghomeIntent);
Intent recentAppIntent = new Intent(Context.NOTIFICATION_SERVICE);
Bundle recentAppBundle = new Bundle();
recentAppBundle.putInt("userAnswer", 2);
recentAppIntent.putExtras(recentAppBundle);
PendingIntent pendingrecentAppIntent =
PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
Intent settingsIntent = new Intent(Context.NOTIFICATION_SERVICE);
Bundle settingsBundle = new Bundle();
settingsBundle.putInt("userAnswer", 3);
settingsIntent.putExtras(settingsBundle);
PendingIntent pendingsettingsIntent =
PendingIntent.getBroadcast(getBaseContext(), 2, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.settingsButt, pendingsettingsIntent);
notificationManager.notify(1, notification);
and onReceive() code is
public void onReceive(Context context, Intent intent) {
Bundle answerBundle = intent.getExtras();
int userAnswer = answerBundle.getInt("userAnswer");
if(userAnswer == 1) {
Log.v("shuffTest","Pressed Home");
}else if(userAnswer == 2) {
Log.v("shuffTest","Pressed Recent App");
}else if(userAnswer == 3) {
Log.v("shuffTest","Pressed Settings");
}
}
回答3:
before two days i was stuck on same problem,i found that i was not specifiying seperate action for every pending intent, like
Intent recentApIntent = new Intent(Context.NOTIFICATION_SERVICE);
recentApIntent.setAction("Action 1");
Bundle recentAppBundle = new Bundle();
recentAppBundle.putInt("userAnswer", 2);
recentAppIntent.putExtras(recentAppBundle);
PendingIntent pendingrecentAppIntent =
PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
Similarly separate each intent with action and when receive broadcast identify intent.getAction() and perform task
来源:https://stackoverflow.com/questions/21911744/handle-three-buttons-click-event-in-custom-notification-in-android