android - 如何向具有 Intent 的 Activity 发送额外内容?

标签 android android-intent

我有一个 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/

相关文章:

java - Android - 将 TXT 文件作为电子邮件附件发送失败 ("Couldn' t 发送附件")

android - 三星 GT-P5100 的计算器包名称是什么?

android - 如何通过 Android 中的 Intent 发送邮件?

android - locationManager.getBestProvider

java - 放大 Android 后模糊谷歌地图 v2

android - 不为联系人调用 onActivityResult

flutter - android_intent Flutter插件,我想从另一个应用程序启动一个应用程序,并使用android_intent库传递一些参数

android - 无法访问 fragment 上的 android.arch.lifecycle.LifecycleOwner?

android - 我怎样才能像谷歌地图一样在 Android 中获得连续的位置更新?

android - 应用程序如何在没有 root 的情况下访问 Android 6.0(API 级别 23)中 USB OTG 存储上的文件?