为Android通知确定addAction单击

Tof*_*ira 60 notifications android broadcastreceiver android-intent android-4.0-ice-cream-sandwich

我正在尝试使用新的通知界面.我已经为通知添加了3个按钮,并且我想在点击每个按钮后将其保存到我的数据库中.

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

我用a 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);
Run Code Online (Sandbox Code Playgroud)

这是 - 的代码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");
    }

}           
}
Run Code Online (Sandbox Code Playgroud)

BroadcastReceiver在Manifest 注册了.另外,我想提一下,BroadcastReceiver当我点击通知中的一个按钮时调用它,但意图总是包含额外的'2'.

这是通知iteslf - 通知

And*_*rry 113

这是因为您将FLAG_UPDATE_CURRENT与具有相同操作的Intent一起使用

来自文档:

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

当您指定pendingIntentMaybe和时pendingIntentNo,系统使用PendingIntent创建的for pendingIntentYes,但它会覆盖额外内容.因此,所有三个变量都指向同一个对象,并且指定的最后一个额外值是for 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);
Run Code Online (Sandbox Code Playgroud)

你的接收者:

@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");
    }
}           
Run Code Online (Sandbox Code Playgroud)

  • 我猜是YES_ACTION,NO_ACTION和MAYBE_ACTION是常量 (3认同)
  • 我有一个疑问需要澄清。我正在使用一个简单的服务类来启动、暂停和停止服务。因此,如果我单击即播放以暂停图像或反之亦然,有一个图像按钮将更改其可绘制图像。在上面的代码中,您提到了包含可绘制对象的 mBuilder.addAction 。我无法弄清楚图像的 id 在哪里?是否可以有两个可绘制对象但只能用于一个图像按钮。 (2认同)
  • 我在YES_ACTION.equals(动作)中收到错误为什么?如何解决这个问题. (2认同)
  • 请更新您的答案,未解释“(YES_ACTION)”属性。 (2认同)

小智 16

一步步

步骤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());
}
Run Code Online (Sandbox Code Playgroud)

步骤1.5

我创建了一个全局类AppConstant

 public class AppConstant 
   {
  public static final String YES_ACTION = "YES_ACTION";
  public static final String STOP_ACTION = "STOP_ACTION";
  } 
Run Code Online (Sandbox Code Playgroud)

第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();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

第3步

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

        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)


tou*_*dra 10

在我的情况下,它添加了intent-filter后对我有用

 <receiver android:name=".AlarmReceiver">
      <intent-filter>
           <action android:name="YES_ACTION"/>
           <action android:name="NO_ACTION"/>
           <action android:name="MAYBE_ACTION"/>
      </intent-filter>
  </receiver>
Run Code Online (Sandbox Code Playgroud)