我想使用PendingIntent传递一个长值,像这样
Intent intentForPending = new Intent(context, NewBlaBlaActivity.class);
long courseId = 15252;
intentForPending.putExtra("courseId", courseId);
intentForPending.putExtra("isFromPushNotification", true);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentForPending, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.appicon)
.setContentTitle("BlaBla")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
并得到如下值
Intent intent = getIntent();
if(intent.getBooleanExtra("isFromPushNotification", false)) {
long courseId = intent.getLongExtra("courseId", 0);
}
但是我总是从意图中得到0。奇怪的是,虽然我可以从意图中使用'isFromPushNotification'键获取布尔值,但是我无法从同一意图中获取long值。
这让我发疯。如您所见,这是一个PushNotification,当我点击通知时,此代码将运行。
我一直尝试着从论坛和stackoverflow中的问题中获取所有方法,在def上添加L后缀,在长对象上添加原始值。但是我认为PendingIntent是湿毯。
我在等你的忠告。谢谢!
最佳答案
如果要在PendingIntent
中使用Extras,请始终在PendingIntent
工厂方法的最后一个参数(在您的情况下为getActivity()
)中使用标志。可能您希望FLAG_UPDATE_CURRENT
表示用新的附加内容替换任何现有的PendingIntent
内容。