我如何通过一个活动传递从一个BroadcastReceiver数据正在启动? [英] How do I pass data from a BroadcastReceiver through to an Activity being started?

查看:121
本文介绍了我如何通过一个活动传递从一个BroadcastReceiver数据正在启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,它需要被零星全天醒了。

I've got an Android application which needs to be woken up sporadically throughout the day.

要,我使用的AlarmManager建立一个PendingIntent,并有该触发一个BroadcastReceiver做到这一点。这BroadcastReceiver的然后开始了活动带来的UI到前台。

To do this, I'm using the AlarmManager to set up a PendingIntent and have this trigger a BroadcastReceiver. This BroadcastReceiver then starts an Activity to bring the UI to the foreground.

以上所有似乎工作,在活动正常启动本身;但我想的BroadcastReceiver的通知,这是由报警器开始(而不是用户所启动)的活动。要做到这一点,我想,从的BroadcastReceiver的的onReceive()方法的意图的额外软件包设置变量,是这样的:

All of the above seems to work, in that the Activity launches itself correctly; but I'd like the BroadcastReceiver to notify the Activity that it was started by the alarm (as opposed to being started by the user). To do this I'm trying, from the onReceive() method of the BroadcastReceiver to set a variable in the extras bundle of the intent, thus:

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

在我活动的onResume()方法,我再找找这个布尔变量的存在:

In the onResume() method of my Activity, I then look for the existence of this boolean variable:

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。 - 我似乎没有能够通过传递任何额外的都在这个包

The getIntent().getExtras() call in onResume() always returns null - I don't seem to be able to pass any extras through at all in this bundle.

如果我用同样的方法来绑定群众演员然而其触发的BroadcastReceiver的PendingIntent,群众演员来通过就好了。

If I use the same method to bind extras to the PendingIntent which triggers the BroadcastReceiver however, the extras come through just fine.

谁能告诉我什么是关于传递一个包从一个BroadcastReceiver到活动不同的,而不是通过包从活动到一个BroadcastReceiver?我害怕我可能会做一些非常非常明显的错在这里...

Can anyone tell me what's different about passing a bundle from a BroadcastReceiver to an Activity, as opposed to passing the bundle from an Activity to a BroadcastReceiver? I fear I may be doing something very very obvious wrong here...

推荐答案

只是为了说清楚(因为我用了很多时间搞清楚如何使它工作)

Just to make it clear (because I used a lot of time figuring out how to make it work)

在扩展的BroadcastReceiver 服务类。把下面的code。在的onReceive()

In the service class that extends BroadcastReceiver. Put in the following code in 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,它不包含额外的价值。你必须赶上他们在 onNewIntent()称为另一种方法 YourActivity。

The FLAG_ACTIVITY_SINGLE_TOP makes sure the apps doesn't re-open if already open. This means that the "old" intent that opened YourActivity in the first place is re-used and it will NOT contain the extra values. You have to catch them in another method called onNewIntent() in 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()或onRestart() - > ONSTART()
请参见: <一个href="http://www.anddev.org/images/android/activity_lifecycle.png">http://www.anddev.org/images/android/activity_lifecycle.png

Please note the two if statements - the onResume() does not know if it's called after OnCreate()->OnStart() OR onRestart()->onStart()
Please see: http://www.anddev.org/images/android/activity_lifecycle.png

这只是用于测试是否应用程序被用户(意向没有额外)上市或由的BroadcastReceiver (意向增强版)。

It's just used to test if the app is launched by the user (intent with no extras) OR by the BroadcastReceiver (intent with extras).

这篇关于我如何通过一个活动传递从一个BroadcastReceiver数据正在启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆