How do I pass data from a BroadcastReceiver through to an Activity being started?

独自空忆成欢 提交于 2019-11-27 12:31:07
Norfeldt

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

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);

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

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

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).

This BroadcastReceiver then starts an Activity to bring the UI to the foreground.

This may be the crux of your problem here. Try overriding onNewIntent() and seeing if the Intent passed to it has your extra. If so, that's because of the way you set up the activity in the manifest (e.g., you're using singleTop) and the fact that, in this specific case, the activity already existed.

You might also consider getting rid of i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); and see if that changes matters.

The upshot is that what you're doing -- putting the extra in the Intent for startActivity() -- should work just fine. In fact, you can see examples of that here. This suggests it's something funky about the activity (e.g., singleTop) or the way you're invoking the activity (e.g., FLAG_ACTIVITY_NEW_TASK).

EDIT:

Since my first shots didn't work, and given your "curiouser" comment above...

This feels a bit like a PendingIntent -- with those, unless you take steps, you will not be able to update extras.

On a whim, try adding a second <intent-filter> to your activity in the manifest, just on some unique action string, and try starting your activity from your receiver using that. Or, just toss some action string into your Intent that the receiver is using for startActivity(), without messing with the manifest.

Instead of getIntent().getExtras().getBoolean(wakeupKey) it is more conventional to write getIntent().getBooleanExtra(wakeupKey, defaultValue). I can't be sure if this is related to your problem, but there is some stuff to do with creating a bundle inside getExtras() that I'm not sure about, so it might be worth a go anyway.

Set flag SingleTop works (don't mix with other flags)

Intent intent = new Intent(ContextManager.getContext(),OrderList.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();       

bundle.putString("username",username);
bundle.putString("password",password);

intent.putExtras(bundle);
startActivityForResult(intent,0); 

Just override the onNewIntent like this and the Bundle var will be available in the onresume method:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!