Android Notification PendingIntent Extras null [英] Android Notification PendingIntent Extras null
问题描述
我正在尝试将信息从通知发送到调用的活动,而从我的活动中我得到了 null.
I am trying to send information from notification to invoked activity, while from my activity I got null.
通知代码为:
private void showNotification() {
Intent resultIntent = new Intent(this, MainActivity.class);
if (D)
Log.d(TAG, "Id: " + Id);
resultIntent.putExtra("ineedid", deviceId);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MeterActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
// Bundle tmp = resultIntent.getExtras();
// if (tmp == null) {
// Log.d(TAG, "tmp bundle is null");
// } else {
// long id = tmp.getLong("ineedid", -1);
// Log.d(TAG, "tmp id : " + id);
// }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
BLEMessengerService.this)
.setSmallIcon(R.drawable.ic_action_search)
.setContentTitle("Event tracker")
.setContentText("Events received").setOngoing(true)
.setContentIntent(resultPendingIntent)
.setWhen(System.currentTimeMillis());
int mId = R.string.service_notification_start_service;
mNM.notify(mId, mBuilder.getNotification());
}
用于从主要活动中的意图获取信息的代码;
Code for get information from intent in main activity;
Bundle extras = getIntent().getExtras();
if (extras != null) {
long deviceID = getIntent().getLongExtra("ineedid",
-1);
if (ID == -1) {
if (D)
Log.i(TAG_D, "Wrong Id received.");
finish();
} else {
device = dataSource.getDeviceByID(deviceID);
if (D)
Log.i(TAG_D, "Get the id.");
}
} else {
if (D)
Log.d(TAG_D, "Bundle is null");
finish();
}
在通知得到通知之前我已经验证过,bundle 不为空,并且它在 extras 中有 id.而当我试图从意图中获取它时,它消失了.帮助.
I have verified before the notification get notified, bundle is not null, and it has id in extras. While, when I tried to fetch it from intent, it's gone. Help.
推荐答案
我刚刚得到答案,添加行:resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
I just got the answer,
add line: resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
注意:如果您将其添加为 resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
它不会工作.
NOTICE: if you add it as resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
It won't work.
我还尝试了其他标志,例如FLAG_ACTIVITY_NEW_TASK"和FLAG_ACTIVITY_RESET_TASK_IF_NEEDED".两者都不在这里.
I also tried other flags like, "FLAG_ACTIVITY_NEW_TASK" and "FLAG_ACTIVITY_RESET_TASK_IF_NEEDED". neither works here.
这篇关于Android Notification PendingIntent Extras null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!