我有一个 BroadcastReceiver 用于监听传入的短信。当一条新短信到达时,它会显示一条通知,用户可以点击它并打开应用程序。我想将消息文本传递给我的 Activity 。
在我的接收器中:
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);
在我的 Activity 中:
@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 总是空的。
有什么想法吗?
最佳答案
我看不到您在哪里将 bundle 添加到 Intent 中。你需要做这样的事情:
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);
关于android - 如何向具有 Intent 的 Activity 发送额外内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591624/