2011-05-22 45 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()返回首次啓動它的意圖。