getIntent()的Extras始终为NULL

77

我编写了一个简单的Android应用程序,显示自定义通知如下:

Context context = getApplicationContext();          
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis());  
Intent notificationIntent = new Intent( context,  this.getClass()); 
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE"); 
PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0);               
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar);
notification.contentIntent = pendingIntent;

notification.contentView.setTextViewText(R.id.notifypb_status_text, text);
notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false);

manager.notify(104, notification);

这段代码只在我的应用程序中调用一次,它显示一个带有进度条的通知(一切都正确)。

现在,当用户单击此通知时,我的应用程序会处理 onResume 事件。

public void onResume()
{
    super.onResume();
    // TODO: Extras è SEMPRE NULL!!! impossibile!
    Intent callingintent = getIntent(); 
    Bundle extras = callingintent.getExtras();

但是 extras 总是为 NULL!

我尝试了各种组合:

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");
或者
Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);

但是getIntent().getExtras()总是返回NULL。


你尝试在通知中设置应用程序上下文了吗? - apesa
5个回答

122
这是情景:
方法getIntent()返回启动活动的第一个意图。

因此,当活动被关闭(终止)并且用户点击通知时,它将运行活动的新实例,并且getIntent()按预期工作(Extras不是null)。

但是,如果活动处于“休眠”状态(在后台),并且用户点击通知,则getIntent()始终返回启动活动的第一个意图,而不是通知意图。

因此,在应用程序正在运行时捕获通知意图,只需使用以下内容:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

然后重写onNewIntent(Intent newintent)

因此,在应用程序首次运行时,可以使用getIntent(),而在从睡眠状态恢复应用程序时,可以使用onNewIntent


44
以下是我的翻译:这个对我有效:PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)。 - user672009
在我的情况下,在启动器活动中添加更多的android:launchMode =“singleTask”。谢谢! - Cuong Nguyen

101

只需在您的Resume()方法顶部编写此代码。这就是所需的全部内容。这会刷新意图 - 我不太清楚,但它有效。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

22

问题:您正在为待处理的意图发送相同的请求码。请更改。

解决方法:设置全局变量 int UNIQUE_INT_PER_CALL =0,并在创建待处理意图时按照以下方式调用。

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
UNIQUE_INT_PER_CALL++; // to increment.

4

看起来你的活动已经在运行了,我认为你需要指定FLAG_UPDATE_CURRENT,否则getIntent()调用将返回先前的。请参见此答案


然而,在我的应用程序中,我没有“PREVIOUS ONE”意图。那段代码只会被调用一次,之后不会创建或更新其他意图。 - Magius
所有的活动都是由意图启动的,因此必须有先前的意图。哈哈,我刚看到你自己的答案,那就是我想说的! - dmon
1
这是解决我的方法。在这里设置标志:http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT - finiteloop

-10

我不需要将数据存储到首选项中。我使用共享首选项进行其他用途。我需要通过Intent传递ExtraData。 - Magius
实际上,这是唯一对我有效的答案。在我的情况下,意图是使用activity:scheme从url启动的,我尝试了所有方法,包括使用savedInstanceState但都没有成功。 - Dohab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接