android - getLongExtra 始终使用 PendingIntent 返回默认值

标签 android android-intent bundle android-pendingintent extras

我想使用类似这样的 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/

相关文章:

android - 自动缩放在手机屏幕上工作正常,但在平板电脑上不行?

android - Unresolved reference BuildConfig

java - 如何使用意图检索双数据?

android - 如何隐藏/取消默认传入屏幕

android - 从另一个应用程序控制一个应用程序

ruby-on-rails - `require' : cannot load such file -- ffi (LoadError) `require' : cannot load such file -- ffi (LoadError) when trying to start rails console

适用于 Android、iOS 和 BB 的 ASP.NET Web 服务

xcode - 应用程序包不包含有效标识符

php - 如何从 Symfony2 上的另一个 Bundle 加载实体

android - 预先填充的特里