Can I launch activity from a broadcast receiver

711 views
Skip to first unread message

Jaap

unread,
Jul 6, 2010, 7:21:55 PM7/6/10
to android-platform
When I receive a message in my broadcast receiver I want to start an
activity. However this does not work and I get a Force Close message.
I am starting the activity with startActivity and if I move the code
outside of the broadcast receiver everything works fine.

Is it not allowed to start an activity in a broadcast receiver?
Any tips on how to start an activity in that case based on data I
receive in the broadcast receiver

Thanks

Jaap

je...@swank.ca

unread,
Jul 6, 2010, 7:49:52 PM7/6/10
to android-platform
On Jul 6, 4:21 pm, Jaap <jaap.hait...@gmail.com> wrote:
> When I receive a message in my broadcast receiver I want to start an
> activity.

You can use an intent to trigger an activity from a broadcast
receiver. I used the following snippet in my own app:

public void onReceive(Context context, Intent intent) {
Intent startActivity = new Intent();
startActivity.setClass(context, MyActivity.class);
startActivity.setAction(MyActivity.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(startActivity);
}

A full example is here:
http://code.google.com/p/publicobject/source/browse/shush/src/com/publicobject/shush/OnRingerMuted.java

Cheers,
Jesse

Jaap

unread,
Jul 7, 2010, 2:36:39 AM7/7/10
to android-platform
> A full example is here:http://code.google.com/p/publicobject/source/browse/shush/src/com/pub...
>
Thanks a lot. The critical part makes it work is setting the flag
FLAG_ACTIVITY_NEW_TASK

Jaap
Reply all
Reply to author
Forward
0 new messages