android - 如何将数据从 BroadcastReceiver 传递到正在启动的 Activity?

标签 android android-intent bundle broadcastreceiver alarm

我有一个 Android 应用程序需要在一天中偶尔被唤醒。

为此,我使用 AlarmManager 设置一个 PendingIntent 并让它触发一个 BroadcastReceiver。这个 BroadcastReceiver 然后启动一个 Activity 将 UI 带到前台。

以上所有似乎都有效,因为 Activity 正确启动;但我希望 BroadcastReceiver 通知 Activity 它是由警报启动的(而不是由用户启动的)。为此,我尝试从 BroadcastReceiver 的 onReceive() 方法在 Intent 的附加包中设置一个变量,因此:

    Intent i = new Intent(context, MyActivity.class);
    i.putExtra(wakeupKey, true);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

在我的 Activity 的 onResume() 方法中,我然后寻找这个 bool 变量是否存在:

protected void onResume() {
    super.onResume();

    String wakeupKey = "blah";      
    if (getIntent()!=null && getIntent().getExtras()!=null)
        Log.d("app", "onResume at " + System.currentTimeMillis() + ":" + getIntent().getExtras().getBoolean(wakeupKey));
    else
        Log.d("app", "onResume at " + System.currentTimeMillis() + ": null");
}

onResume() 中的 getIntent().getExtras() 调用总是返回 null - 我似乎无法在此包中传递任何额外内容。

但是,如果我使用相同的方法将 extras 绑定(bind)到触发 BroadcastReceiver 的 PendingIntent,extras 就可以顺利通过。

谁能告诉我将包从 BroadcastReceiver 传递到 Activity 与将包从 Activity 传递到 BroadcastReceiver 有什么不同?我担心我可能会在这里做一些非常非常明显的错误......

最佳答案

只是为了说清楚(因为我花了很多时间弄清楚如何让它工作)

在扩展 BroadcastReceiver 的服务类中.在onReceive()中输入以下代码

Intent intent2open = new Intent(context, YourActivity.class);
intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String name = "KEY";
String value = "String you want to pass";
intent2open.putExtra(name, value);
context.startActivity(intent2open);

FLAG_ACTIVITY_SINGLE_TOP确保应用程序在已经打开的情况下不会重新打开。这意味着首先打开 YourActivity 的“旧” Intent 将被重新使用,并且它不会包含额外的值。您必须使用另一种名为 onNewIntent() 的方法来捕获它们在 YourActivity.

public class YourActivity extends Activity {
    private String memberFieldString;

    @Override
    public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         // Code doing your thing...
    } // End of onCreate()

    @Override
    protected void onNewIntent(Intent intent) {
    Log.d("YourActivity", "onNewIntent is called!");

    memberFieldString = intent.getStringExtra("KEY");

    super.onNewIntent(intent);
} // End of onNewIntent(Intent intent)

    @Override
    protected void onResume() {
        if (memberFieldString != null) {
            if (opstartsIntent.getStringExtra(KEY) != null) {
               Log.d("YourActivity", "memberFieldString: "+ memberFieldString);
            } else {
               Log.d("YourActivity", "The intent that started YourActivity did not have an extra string value");
            }
        }
    } // End of onResume()

}  // End of YourActivity

请注意两个 if 语句 - onResume()不知道是不是在OnCreate()->OnStart() OR onRestart()->onStart()之后调用的
请参阅: http://www.anddev.org/images/android/activity_lifecycle.png

它只是用来测试应用程序是由用户启动的(没有额外的 Intent )还是由 BroadcastReceiver 启动的(有额外的 Intent )。

关于android - 如何将数据从 BroadcastReceiver 传递到正在启动的 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2616859/

相关文章:

android - 如何通过条形码获取信息?

android - 应用程序背景资源路径的最佳方式

java - Android:总是在 Intent 中传递的额外内容中接收空对象

java - 使用 Linkify Android 打开 Activity

android - 使用导航架构时如何将数据传递到第一个 fragment ?

android - ListView 中的 performItemClick 不突出显示项目

android 应用程序小部件动态背景颜色与圆角 android 2.3

android - 为什么我不能在 Android N+ 中附加文件?

带有 bundle 的 Android 奇怪行为

bundle - 如何让M2E eclipse插件理解Bundle打包