从通知的意图没有额外 [英] Intent from notification does not have extras

查看:116
本文介绍了从通知的意图没有额外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个普遍的问题,我经历了所有相关的问题,我能找到已经去了: 活动不是拿起新的意图,<一个href="http://stackoverflow.com/questions/10456655/why-extra-data-integer-is-not-sent-in-android-notification-intent">Why额外的数据(整数)是不是在Android的通知的意图送?,<一个href="http://stackoverflow.com/questions/7370324/notification-passes-old-intent-extras/9330144">Notification通行证老意图额外的,<一个href="http://stackoverflow.com/questions/7098893/cant-put-extras-for-an-intent-in-notification">Can't把临时演员的意图通知,<一个href="http://stackoverflow.com/questions/3009059/android-pending-intent-notification-problem">android悬而未决的意图通知问题;但还是不知道这一点。

This seem to be a common problem and I went through all the related questions I could find already: Activity isn't picking up new intent, Why extra data (integer) is not sent in android notification intent?, Notification passes old Intent Extras, Can't put extras for an intent in notification, android pending intent notification problem; but still cannot figure this out.

问题是相同的。我设置了通知,其中一个PendingIntent携带一些额外的信息,我不明白这一点的另一边。

Problem is the same. I set a notification with a PendingIntent carrying some extra information and I don't get it on the other side.

下面是$ C $下产生的通知:

Here is the code for generating the notification:

Notification notification = new Notification(R.drawable.icon, getResources().getString(R.string.notification_ticker), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;

Intent start_test = new Intent(this, MyActivity.class);
start_test.putExtra("start_test", true);
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), start_test, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content, expired), pi);
nm.notify(NOTIFICATION_ID, notification);

而在另一面:

And on the other side:

boolean start_test=getIntent().getBooleanExtra("start_test", false);

这个捆绑其实是不存在(getExtras()返回null)。

The bundle is actually not there (getExtras() returns null).

我尝试了不同的标志 PendingIntent.getActivity (<$ C C $> FLAG_UPDATE_CURRENT , FLAG_CANCEL_CURRENT FLAG_ONE_SHOT ),没有这些帮助的。

I tried the different flags for PendingIntent.getActivity (FLAG_UPDATE_CURRENT, FLAG_CANCEL_CURRENT, FLAG_ONE_SHOT), none of those helped.

如图所示code,我用getCurrentMillis,以确保请求ID的变化......

As shown in the code, I use getCurrentMillis to make sure the requestID changes....

还发现,在MyActivity,的onCreate onNewIntent 不获取调用。仅 onResume 是。尽管 FLAG_ACTIVITY_NEW_TASK 设置...?

Also found that in MyActivity, onCreate and onNewIntent are not getting called. only onResume is. Even though the FLAG_ACTIVITY_NEW_TASK is set... ?

我缺少的东西很简单?

推荐答案

设置 FLAG_ACTIVITY_NEW_TASK 的通知意向将导致以下后果:

Setting FLAG_ACTIVITY_NEW_TASK for the notification Intent will cause the following:

  • 如果该活动的不可以的任务已在运行的新任务将启动,并且意图将的onCreate传递到活动()

  • If the activity is not already running in a task, a new task will be started and the Intent will be delivered to the activity in onCreate()

但是,如果该活动是一个任务已经运行,该任务将被带到前台。就这样。这样做的目的将不可以交付和 onNewIntent()将不会被调用。

However, if the activity is already running in a task, that task will be brought to the foreground. That's all. The Intent will not be delivered and onNewIntent() will not be called.

如果你想意图的实际交付您需要指定:

If you want the Intent to actually be delivered you need to specify:

start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);

这使得没有什么区别是否对 launchMode 的活动是 singleTop 或不,你还必须在意向书中指定 Intent.FLAG_ACTIVITY_SINGLE_TOP

It makes no difference whether the launchMode of the activity is singleTop or not, you still must specify Intent.FLAG_ACTIVITY_SINGLE_TOP in the Intent.

注意:如果该活动的任务已经运行和活动的不可以在该任务的活动堆栈的顶部,该任务将被带到前景。就这样。这样做的目的将不可以交付。做的唯一方法这一点是添加 Intent.FLAG_ACTIVITY_CLEAR_TOP 其他2标志,但是这可能不是你想要发生的事情(取决于您的具体方案)

Note: If the activity is already running in a task and the activity is not on top of the activity stack in that task, the task will be brought to the foreground. That's all. The Intent will not be delivered. The only way to make this happen would be to add Intent.FLAG_ACTIVITY_CLEAR_TOP to the other 2 flags, but this may not be what you want to happen (depends on your specific scenario).

在<一见我(仍然)在谷歌code未决问题href="http://$c$c.google.com/p/android/issues/detail?id=17137">http://$c$c.google.com/p/android/issues/detail?id=17137

这篇关于从通知的意图没有额外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆