2012-03-06 62 views
1

我有一個BroadcastReceiver偵聽傳入的短消息。當新的短信到達時,它會顯示一條通知,用戶可以點擊它並打開該應用。我想將消息文本傳遞給我的活動。如何向intents發送附加內容?

在我的接收器:

Notification notifacation = new Notification(); 
notifacation.icon = icon; 
notifacation.tickerText = tickerText; 
notifacation.when = System.currentTimeMillis(); 
Bundle bundle = new Bundle(); 
bundle.putString("SMS_PARAMETERS", smsParameters); 

Intent notificationIntent = new Intent(context, MyActivity.class); 
notificationIntent.putExtra("SMS_PARAMETERS", bundle); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
notifacation.setLatestEventInfo(context, tickerText, message, contentIntent); 
notificationManager.notify(1, notifacation); 

裏面我的活動:

@Override 
public void onResume() { 
    Bundle bundle = this.getIntent().getExtras(); 
    if(bundle != null) { 
     String smsParameters = bundle.getString("SMS_PARAMETERS"); 
     Log.d(DEBUG_TAG, "SMS_PARAMETERS: " + smsParameters); 
     Toast.makeText(this, smsParameters, Toast.LENGTH_LONG).show(); 
    }  
super.onResume(); 
} 

的問題是,束總是空。

有什麼想法?

+0

之前,我想了很多,這是大大錯誤添加這一行。您不應該在onResume()中獲取包,否則notificationmanager會在不更改Bundle或setLatestEventInfo的情況下處理通知。你的代碼永遠不會顯示你真的將包傳遞給了意圖?我會回答你的問題,但我不確定你是否在問正確的事情。傳遞給intent:Intent i = new Intent(MYCLASS.class); i.putExtra(「KEY」,1); startActivity(ⅰ);然後在MYCLASS.class的onCreate中檢索額外的this.getIntent()。getExtras()。getInt(「KEY」); – bbedward 2012-03-06 20:56:40

回答

0

我終於找到了答案: 我應該startActivity(...)

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
2

我沒有看到你添加包的意圖。你需要做的是這樣的:

Bundle bundle = new Bundle(); 
bundle.putString("SMS_PARAMETERS", smsParameters); 


Intent notificationIntent = new Intent(context, CarMapActivity.class); 
notificationIntent.putExtras(bundle); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
notifacation.setLatestEventInfo(context, tickerText, message, contentIntent); 
notificationManager.notify(1, notifacation); 
+0

Intent類中沒有方法putExtras(Bundle b)(我使用的是Android 2.1 API級別7)。我改變了這樣的代碼:i.putExtras(「SMS_PARAMETERS」,smsParameters),但我仍然在Activity中得到null。 – 2012-03-06 21:01:32

+0

嘗試修復您的導入和重建。我在我的代碼中多次使用'.putExtras(bundle);'確切的方法。這裏是它的文檔:http://developer.android.com/reference/android/content/Intent.html#putExtras(android.os.Bundle) – Woodsy 2012-03-06 21:07:14

+0

由於某種原因,我無法發佈上述正確的鏈接。請添加一個)到鏈接的末尾,它會帶你到頁面的正確部分 – Woodsy 2012-03-06 21:09:48