我正在尝试使用新的通知界面。我在通知中添加了 3 个按钮,我想在单击每个按钮后将一些内容保存到我的数据库中。
通知本身运行良好,并在调用时显示,我只是不知道如何捕获三个不同按钮单击中的每一个。
我正在使用 BroadcastReceiver
来捕捉点击,但我不知道如何判断点击了哪个按钮。
这是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");
}
}
}
我已经在 Manifest 中注册了 BroadcastReceiver
。
另外,我想提一下,当我单击通知中的一个按钮时会调用 BroadcastReceiver
,但 Intent 始终包含额外的“2”。
这是通知iteslf -
最佳答案
这是因为您使用的是 FLAG_UPDATE_CURRENT具有相同 Action 的 Intent
来自文档:
if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.
当您指定 pendingIntentMaybe
和 pendingIntentNo
时,系统使用为 pendingIntentYes
创建的 PendingIntent
,但它会覆盖额外的。因此,所有三个变量都引用同一个对象,并且最后指定的附加对象是 pendingIntentNo
。
您应该为每个 Intent
指定一个替代操作。您仍然可以拥有一个 BroadcastReceiver
,并让它拦截所有三个 Action 。这在语义上也不会那么困惑:)
您的通知海报:
//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");
}
}
关于android - 确定 Android 通知的 addAction 点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15350998/