56

我正在尝试使用新的通知界面。我在通知中添加了3个按钮,并且每次单击它们时我都会将某些内容保存到我的数据库中。确定Android通知的addAction点击

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

我使用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"); 
    } 

}   
} 

我已经在清单中注册了BroadcastReceiver。 另外,我想提到的是,当我单击通知中的某个按钮时,会调用BroadcastReceiver,但意图总是包含额外的'2'。

这是notifcation iteslf - notification

+0

这是奇怪。我在代码中看不到任何错误。如果你改变addAction()的顺序(和PendingIntent创建),你还会得到'2'吗? – Quanturium 2013-03-12 01:24:39

+0

在这种情况下,它只是给了我最后一个addAction()我叫 – Tofira 2013-03-12 08:59:46

+0

我可以为三个按钮中的每一个注册三个BroadcastReceivers,但我真的想避免这种情况。 – Tofira 2013-03-12 09:32:18

回答

103

这是因为你使用FLAG_UPDATE_CURRENT与具有相同的作用

从文档意图:

如果所描述的PendingIntent已经存在,然后保留它,但是用这个新的Intent替换它的额外数据。

当您指定pendingIntentMaybependingIntentNo,系统将使用pendingIntentYes创建PendingIntent,但它覆盖的花絮。因此,所有三个变量都指向相同的对象,并且最后指定的附加值为pendingIntentNo

您应该为每个Intent指定一个替代操作。你仍然可以有一个BroadcastReceiver,并且只需要拦截所有三个动作。这将是减少混乱语义以及:)

您的通知海报:

//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"); 
    } 
}   
+1

我有一个疑问,我需要清除。我正在使用一个简单的服务类来启动,暂停和停止服务。因此,如果单击即暂停图像或vise-verse,则会有一个图像按钮将更改其图像的可绘制性。在上面的代码中,您提到了包含drawable的mBuilder.addAction。我无法弄清楚图像的id在哪里?有可能有两个可绘制的按钮,但只能用于一个图像按钮。 – 2013-07-18 21:09:48

+2

我在YES_ACTION.equals(动作)中遇到错误原因?如何解决这个问题。 – 2013-07-19 06:24:09

+0

在哪里以及如何设置YES_ACTION,NO_ACTION ...等等...那么显性文件看起来像 – Cripto 2013-12-29 05:35:42

10
在我的情况下,它加入的意图过滤

后为我工作

<receiver android:name=".AlarmReceiver"> 
     <intent-filter> 
      <action android:name="YES_ACTION"/> 
      <action android:name="NO_ACTION"/> 
      <action android:name="MAYBE_ACTION"/> 
     </intent-filter> 
    </receiver> 
+1

最佳做法(y) – 2015-12-31 12:59:43

2

here YES_ACTION必须是yourfullpackagename.YES

private static final String YES_ACTION = "com.example.packagename.YES"; 

同样可以使用NO_ACTIONMAYBE_ACTION

在广播接收器必须使用相同的YES_ACTION如上声明,

意味着广播接收器类,你可以检查定制的广播以下

public class NotificationReceiver extends BroadcastReceiver { 

private static final String YES_ACTION = "com.example.packagename.YES"; 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    String action = intent.getAction(); 
    if(YES_ACTION.equals(action)) { 
     Toast.makeText(context, "CALLED", Toast.LENGTH_SHORT).show(); 
    } 
} 

}

注意:在YES_ACTION字符串中,您也可以使用其他字。

8

STEP_BY_STEP

步骤1

public void noto2() // paste in activity 
{ 
    Notification.Builder notif; 
    NotificationManager nm; 
    notif = new Notification.Builder(getApplicationContext()); 
    notif.setSmallIcon(R.drawable.back_dialog); 
    notif.setContentTitle(""); 
    Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    notif.setSound(path); 
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    Intent yesReceive = new Intent(); 
    yesReceive.setAction(AppConstant.YES_ACTION); 
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
    notif.addAction(R.drawable.back_dialog, "Yes", pendingIntentYes); 


    Intent yesReceive2 = new Intent(); 
    yesReceive2.setAction(AppConstant.STOP_ACTION); 
    PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
    notif.addAction(R.drawable.back_dialog, "No", pendingIntentYes2); 



    nm.notify(10, notif.getNotification()); 
} 

步骤1.5

我创建全局类AppConstant

public class AppConstant 
    { 
    public static final String YES_ACTION = "YES_ACTION"; 
    public static final String STOP_ACTION = "STOP_ACTION"; 
    } 

步骤2:

public class NotificationReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    String action = intent.getAction(); 
    if (AppConstant.YES_ACTION.equals(action)) { 
     Toast.makeText(context, "YES CALLED", Toast.LENGTH_SHORT).show(); 
    } 
    else if (AppConstant.STOP_ACTION.equals(action)) { 
     Toast.makeText(context, "STOP CALLED", Toast.LENGTH_SHORT).show(); 
    } 
} 

}

步骤3

<receiver android:name=".NotificationReceiver"> 
     <intent-filter> 
      <action android:name="YES_ACTION"/> 
      <action android:name="STOP_ACTION"/> 

     </intent-filter> 
    </receiver> 
+0

这样做的工作吗?你有没有测试过这个? – 2017-03-22 06:25:15

+0

是的,它工作的很好,我刚刚测试过它 – ysfcyln 2017-04-29 13:18:12

+0

工作正常,我试过了,没有问题... – Venkat 2017-11-29 08:13:45