【发布时间】:2012-11-15 19:02:40
【问题描述】:
我正在尝试将信息从通知发送到调用的活动,而从我的活动中我得到了 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());
}
从主要活动的意图中获取信息的代码;
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 不为 null,并且在 extras 中有 id。 虽然,当我试图从意图中获取它时,它已经消失了。帮助。
【问题讨论】:
-
我也尝试添加“resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);”,但是没有用。
-
您不使用
resultPendingIntent.getExtras()的原因是什么? -
我只是做了这些行来检查我的 pendingIntent 是否有 Extras,并且我验证了它有我放在那里的内容。
-
如果您使用 FCM,通常通知由 android 本身处理,因此,您的通知在单击时没有额外内容。更多信息,请参考:stackoverflow.com/questions/37358462/…
标签: android android-intent bundle android-notifications android-pendingintent