确定 Android 通知的 addAction 单击 [英] Determine addAction click for Android notifications

本文介绍了确定 Android 通知的 addAction 单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的通知界面.我在通知中添加了 3 个按钮,我想在点击每个按钮后将某些内容保存到我的数据库中.

I'm trying to use the new notifications interface. I've added 3 buttons to the notifications, and I want to save something to my database once each of them is clicked.

通知本身运行良好并在调用时显示,我只是不知道如何捕获三个不同的按钮点击中的每一个.

The notification itself works well and is shown when called, I just don't know how to capture each of the three different button clicks.

我正在使用 BroadcastReceiver 来捕捉点击,但我不知道如何判断点击了哪个按钮.

I'm using a BroadcastReceiver to catch the clicks, but I don't know how to tell which button was clicked.

这是AddAction的代码(我已经排除了通知的其余部分,因为它运行良好) -

This is the code of AddAction(I've excluded the rest of the notification, as its working well) -

    //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,但意图总是包含一个额外的2".

I've registered the BroadcastReceiver in the Manifest. Also, I want to mention that the BroadcastReceiver is called when I click one of the buttons in the notification, but the intent always includes an extra of '2'.

这是通知本身-

推荐答案

这是因为您正在使用 FLAG_UPDATE_CURRENT 具有相同动作的 Intent

It's because you're using FLAG_UPDATE_CURRENT with Intents that have the same action

来自文档:

如果所描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换它的额外数据.

if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.

当您指定pendingIntentMaybependingIntentNo时,系统使用为pendingIntentYes创建的PendingIntent,但它会覆盖额外的.因此,所有三个变量都指向同一个对象,最后指定的附加项用于 pendingIntentNo.

When you specify pendingIntentMaybe and pendingIntentNo, the system uses the PendingIntent created for pendingIntentYes, but it overwrites the extras. Thus, all three variables refer to the same object, and the last extras specified were for pendingIntentNo.

您应该为每个 Intent 指定一个替代操作.您仍然可以拥有一个 BroadcastReceiver,并让它拦截所有三个操作.这在语义上也不会那么混乱:)

You should specify an alternative action for each Intent. You can still have one BroadcastReceiver, and just have it intercept all three actions. This would be less confusing semantically as well :)

您的通知海报:

//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 通知的 addAction 单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆