【问题标题】:getLongExtra always return default value using PendingIntentgetLongExtra 总是使用 PendingIntent 返回默认值
【发布时间】:2014-12-19 14:13:17
【问题描述】:

我想使用类似这样的 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”键获取布尔值,但我无法从同一意图中获取长值。

这让我发疯。如您所见,它是一个 PushNotification,当我点击通知时,此代码就会运行。

我尝试了从论坛和 stackoverflow 中的问题获取的所有方法,在 def 上添加 L 后缀,在长对象上添加原始值。但我认为 PendingIntent 是湿毯子。

我正在等待您的上帝般的建议。谢谢!

【问题讨论】:

  • intent.getIntExtra("courseId", 0) 返回 15252 吗?
  • intent.hasExtra( "courseId" ) 返回什么?

标签: android android-intent bundle android-pendingintent extras


【解决方案1】:

如果您要使用带有PendingIntent 的附加功能,请始终在PendingIntent 工厂方法的最后一个参数中使用标志(在您的情况下为getActivity())。可能您希望FLAG_UPDATE_CURRENT 表示用新的附加内容替换任何现有的PendingIntent 内容。

【讨论】:

  • 这就是神一样的答案。谢谢!
猜你喜欢
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多