我想使用类似这样的 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);
}
但我总是从 Intent 中得到 0。奇怪的是,虽然我可以从 Intent 中使用“isFromPushNotification”键获取 bool 值,但我无法从同一 Intent 中获取长值。
这让我发疯。如您所见,它是一个 PushNotification,当我点击通知时,此代码就会运行。
我尝试了从论坛和 stackoverflow 中的问题中可以得到的所有方法,在 def 上添加 L 后缀,在长对象上添加原始值。但我认为 PendingIntent 令人扫兴。
我正在等待您的神圣建议。谢谢!
最佳答案
如果您要使用 PendingIntent
的附加功能,始终在 PendingIntent
的最后一个参数中使用标志工厂方法( getActivity()
在你的情况下)。也许你想要 FLAG_UPDATE_CURRENT
,表示替换任何现有的 PendingIntent
包含新附加内容的内容。
关于android - getLongExtra 始终使用 PendingIntent 返回默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27567827/