获取唤醒我的活动的意图

时间:2011-05-22 17:50:20

标签: android

我正在开发一个包含广播接收器的应用程序。 广播接收器设置onReceive()通知,其中包括待处理的Intent

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 Activity中,我在onResume()中收到此Intent:

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

打印:

  • 3:活动被销毁时
  • -1:当活动正在睡觉时

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

所以我的问题是:我怎样才能获得唤醒我活动的意图?

2 个答案:

答案 0 :(得分:4)

  

当活动正在睡觉时

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

  

如何获得唤醒我活动的Intent?

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

答案 1 :(得分:1)

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