2011-05-22 51 views
2

我正在开发包含广播接收器的应用程序。 广播接收机设置的通知的onReceive(),它包括一待决意图获取唤醒我的活动的意图

Intent updateHistoryIntent = new Intent(this, NotificationsHistory.class); 
updateHistoryIntent.putExtra("test", 3); 
PendingIntent updateHistoryPendingIntent 
    = PendingIntent.getActivity(this, 0, updateHistoryIntent, 0); 

Notification notification 
    = new Notification(icon, contentTitle, System.currentTimeMillis()); 
notification.setLatestEventInfo(
    context, contentTitle, 
    contentText, updateHistoryPendingIntent 
); 

在NotificationsHistory活性,我接收的onResume()与此意图:

int testInt = this.getIntent().getIntExtra("test", -1); 
Aux.log("test : " + testInt); 

它打印:

  • 3:当活动被破坏
  • -1:当活动正在睡觉

如果我正确阅读文档,this.getIntent()将返回最初启动Activity的Intent,并且我描述的行为与预期的一样。

所以我的问题是:如何获得唤醒我的活动的意图?

回答

4

活动时正在睡觉

活动不睡觉。我假设你的意思是活动在后台。

如何获得唤醒我的活动的意图?

FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP作为标记添加到您的Intent。然后,您的活动将通过onNewIntent()传递给它Intent

+0

非常感谢; (尤其是与以下组合: '保护无效onNewIntent(意图意图){setIntent(意向);}') – dimi 2011-05-22 18:12:04

1

我认为问题是当一个活动被唤醒时,getIntent()返回首次启动它的意图。