确定的addAction点击Android通知(Determine addAction click

2019-07-21 08:30发布

我试图用新的通知界面。 我已经添加了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");
    }

}           
}

我所注册的BroadcastReceiver的清单。 另外,我想提一提的是, BroadcastReceiver ,当我点击通知中的一个按钮叫,但目的始终包括“2”的额外费用。

这是notifcation iteslf -

Answer 1:

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

从文档:

如果所描述的PendingIntent已经存在,则保留它,但它与什么是在这个新的意图替换其额外数据。

当您指定pendingIntentMaybependingIntentNo ,本系统采用PendingIntent为创建pendingIntentYes ,但它覆盖的花絮。 因此,所有三个变量指向同一个对象,指定的最后一个演员是为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");
    }
}           


Answer 2:

一步步

步骤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>


Answer 3:

在我的情况下,它的工作对我来说加入意向过滤器后,

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


Answer 4:

这里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字符串是你也可以用其他的字。



文章来源: Determine addAction click for Android notifications