我有一个侦听传入短信的BroadcastReceiver。当新的短信到达时,它会显示一条通知,用户可以点击该短信并打开该应用。我想将消息文本传递给我的活动。
我的接收者:
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();
}
问题是bundle总是为空。
有什么想法吗?
答案 0 :(得分: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);
答案 1 :(得分:0)
最后我找到了答案: 我应该在startActivity(...)
之前添加这一行notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);