我正在尝试使用新的通知界面。我在通知中添加了 3 个按钮,我想在单击每个按钮后将某些内容保存到我的数据库中。
通知本身运行良好,并在调用时显示,我只是不知道如何捕获三个不同按钮单击中的每一个。
我正在使用 aBroadcastReceiver
来捕捉点击,但我不知道如何判断点击了哪个按钮。
这是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");
}
}
}
我已经BroadcastReceiver
在 Manifest 中注册了。另外,我想提一下,BroadcastReceiver
当我单击通知中的一个按钮时会调用它,但意图总是包含额外的“2”。
这是通知iteslf -