2012-03-06 61 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