具有多个操作的多个通知
Multiple notifications with multiple action
我有多个通知,每个通知都有两个按钮(Start
和 Pause
),如下图所示,每个通知都属于一个唯一的用户。
每个通知都有一个基于每个用户名的唯一通知 ID。
我使用 pending intent
和 putExtra
来处理操作按钮,但它不起作用。
putExtra 将发送,useName
对每个用户都是唯一的:
intentPause.putExtra(userName,"pause");
intentResume.putExtra(userName,"start");
我认为问题是我不知道每次单击操作按钮时我正在处理哪个通知,以便从 putExtra 接收字符串的 BroadcastReceiver onReceive 方法。如何在 intentPause.putExtra(userName,"pause");
中获取字符串 userName
以便将其放入 String action = intent.getStringExtra(userName);
抱歉我的英语不好。
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra(userName);//Dont know how to get UserName from PutExtra
if (action.equals("resume")) {
timerResume();
Log.d("Action:", action);
} else if (action.equals("pause")) {
timerCancel();
Log.d("Action:", action);
}
}
public void timerResume(){
MainActivity.timerMap.get(userName).countDownTimer.start();
Log.d("Test resume:",userName);
}
public void timerCancel(){
MainActivity.timerMap.get(userName).countDownTimer.cancel();
Log.d("Test cancel:",userName);
}
基于@alexal1 的回答。我已经试过了,但是 getStringExtra
在 BroadCast Receiver
总是 return NULL
Manifest.xml
<receiver android:name=".NotificationReceiver2">
<intent-filter>
<action android:name="com.tuan.timer.ACTION_PAUSE" />
<action android:name="com.tuan.timer.ACTION_RESUME" />
</intent-filter>
</receiver>
NotificationReceiver2
public class NotificationReceiver2 extends BroadcastReceiver {
public static final String ACTION_PAUSE= "com.tuan.timer.ACTION_PAUSE";
public static final String ACTION_RESUME= "com.tuan.timer.ACTION_RESUME";
public static final String EXTRA_USER_NAME = "user_name";
String userName;
String action;
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
Bundle bundle=intent.getExtras();
//getStringExtra return NULL
Log.d ("myApplication", bundle2string(bundle) + " is a key in the bundle");
Log.d ("get extra user name", intent.getStringExtra(EXTRA_USER_NAME) );
if(action != null){
Log.d("Action: ",action );
}
else{
Log.d("Action: ","is null" );
}
}
MainActivity
intentPause=new Intent(this, notificationReceiver2.class);
intentPause.setAction(NotificationReceiver2.ACTION_PAUSE);
intentPause.putExtra(EXTRA_USER_NAME,userName);
Log.d("Put Extra:",NotificationReceiver2.ACTION_PAUSE);
pendingIntentPause=PendingIntent.getBroadcast(this,notificationId,this.intentPause,PendingIntent.FLAG_UPDATE_CURRENT);
intentResume=new Intent(this, notificationReceiver2);
intentResume.setAction(NotificationReceiver2.ACTION_RESUME);
intentResume.putExtra(EXTRA_USER_NAME,userName);
Log.d("Put Extra:",NotificationReceiver2.ACTION_RESUME);
您似乎没有正确使用 putExtra()
。它不放置两个值,而是按一个键放置一个值。所以,你应该像这样构建你的 Intent
s:
String ACTION_PAUSE = "pause";
String EXTRA_USER_NAME = "user_name";
Intent intentPause = new Intent(this, MyBroadcastReceiver.class);
intentPause.setAction(ACTION_PAUSE); // we put button action in a specific field, not in extras
intentPause.putExtra(EXTRA_USER_NAME, userName);
然后用 PendingIntent
包装这个 Intent
并附加到您的通知:
PendingIntent pendingIntentPause = PendingIntent.getBroadcast(this, 0, intentPause, 0);
在您的 BroadcastReceiver 中首先检查操作类型,然后才从 extras 中获取数据:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_PAUSE)) {
timerResume();
Log.d("Action:", action);
String userName = intent.getStringExtra(EXTRA_USER_NAME);
}
...
}
我有多个通知,每个通知都有两个按钮(Start
和 Pause
),如下图所示,每个通知都属于一个唯一的用户。
每个通知都有一个基于每个用户名的唯一通知 ID。
我使用 pending intent
和 putExtra
来处理操作按钮,但它不起作用。
putExtra 将发送,useName
对每个用户都是唯一的:
intentPause.putExtra(userName,"pause");
intentResume.putExtra(userName,"start");
我认为问题是我不知道每次单击操作按钮时我正在处理哪个通知,以便从 putExtra 接收字符串的 BroadcastReceiver onReceive 方法。如何在 intentPause.putExtra(userName,"pause");
中获取字符串 userName
以便将其放入 String action = intent.getStringExtra(userName);
抱歉我的英语不好。
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra(userName);//Dont know how to get UserName from PutExtra
if (action.equals("resume")) {
timerResume();
Log.d("Action:", action);
} else if (action.equals("pause")) {
timerCancel();
Log.d("Action:", action);
}
}
public void timerResume(){
MainActivity.timerMap.get(userName).countDownTimer.start();
Log.d("Test resume:",userName);
}
public void timerCancel(){
MainActivity.timerMap.get(userName).countDownTimer.cancel();
Log.d("Test cancel:",userName);
}
基于@alexal1 的回答。我已经试过了,但是 getStringExtra
在 BroadCast Receiver
总是 return NULL
Manifest.xml
<receiver android:name=".NotificationReceiver2">
<intent-filter>
<action android:name="com.tuan.timer.ACTION_PAUSE" />
<action android:name="com.tuan.timer.ACTION_RESUME" />
</intent-filter>
</receiver>
NotificationReceiver2
public class NotificationReceiver2 extends BroadcastReceiver {
public static final String ACTION_PAUSE= "com.tuan.timer.ACTION_PAUSE";
public static final String ACTION_RESUME= "com.tuan.timer.ACTION_RESUME";
public static final String EXTRA_USER_NAME = "user_name";
String userName;
String action;
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
Bundle bundle=intent.getExtras();
//getStringExtra return NULL
Log.d ("myApplication", bundle2string(bundle) + " is a key in the bundle");
Log.d ("get extra user name", intent.getStringExtra(EXTRA_USER_NAME) );
if(action != null){
Log.d("Action: ",action );
}
else{
Log.d("Action: ","is null" );
}
}
MainActivity
intentPause=new Intent(this, notificationReceiver2.class);
intentPause.setAction(NotificationReceiver2.ACTION_PAUSE);
intentPause.putExtra(EXTRA_USER_NAME,userName);
Log.d("Put Extra:",NotificationReceiver2.ACTION_PAUSE);
pendingIntentPause=PendingIntent.getBroadcast(this,notificationId,this.intentPause,PendingIntent.FLAG_UPDATE_CURRENT);
intentResume=new Intent(this, notificationReceiver2);
intentResume.setAction(NotificationReceiver2.ACTION_RESUME);
intentResume.putExtra(EXTRA_USER_NAME,userName);
Log.d("Put Extra:",NotificationReceiver2.ACTION_RESUME);
您似乎没有正确使用 putExtra()
。它不放置两个值,而是按一个键放置一个值。所以,你应该像这样构建你的 Intent
s:
String ACTION_PAUSE = "pause";
String EXTRA_USER_NAME = "user_name";
Intent intentPause = new Intent(this, MyBroadcastReceiver.class);
intentPause.setAction(ACTION_PAUSE); // we put button action in a specific field, not in extras
intentPause.putExtra(EXTRA_USER_NAME, userName);
然后用 PendingIntent
包装这个 Intent
并附加到您的通知:
PendingIntent pendingIntentPause = PendingIntent.getBroadcast(this, 0, intentPause, 0);
在您的 BroadcastReceiver 中首先检查操作类型,然后才从 extras 中获取数据:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_PAUSE)) {
timerResume();
Log.d("Action:", action);
String userName = intent.getStringExtra(EXTRA_USER_NAME);
}
...
}