[android-developers] Start an external activity from a BroadcastReceiver

2011-06-09 Thread Marc
Hello folks,

after googling all the day, I have solution to actually start an
external activity from a receiver.
In the end I added a service to the package, so the receiver starts
the service and the service should start the activity. Of course that
doesn't sound very efficient at all, but in addition to this it
doesn't work either. Thats my code from the service:

public void onCreate() {
super.onCreate();
Intent i = new Intent(android.intent.action.MAIN);
ComponentName n = new
ComponentName(com.schwimmer.android.carmode,
com.schwimmer.android.carmode.ToggleCarMode);
i.setComponent(n);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
//this.stopSelf();

If I put this code into an activity and start it wit
this.startActivity() it works like a charm. But not if it is put into
the receiver or server.

Do you have any idea how to fix that?

Thanks!
Marc

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Start an external activity from a BroadcastReceiver

2011-06-09 Thread Dianne Hackborn
You are using hand-written constants all over the place.  The SDK has a
constant for the main action -- Intent.ACTION_MAIN.  There are Intent and
ComponentName constructors to build references to activities through the
Context and their Class.

You don't even need to use ACTION_MAIN if you are just explicitly starting
one of your components.

If you are using getApplication(), then this code will work the same way no
matter where you do it from -- Activity, Service, or Receiver.

There is nothing preventing you from starting an activity from any of these
places, you just need to set Intent.FLAG_ACTIVITY_NEW_TASK if you aren't
launching the activity from an existing activity (since without an activity
you don't have an existing task to launch in to).

It would help if you gave more information -- what errors you are seeing in
the log etc.

On Thu, Jun 9, 2011 at 10:41 AM, Marc marcolebull...@googlemail.com wrote:

 Hello folks,

 after googling all the day, I have solution to actually start an
 external activity from a receiver.
 In the end I added a service to the package, so the receiver starts
 the service and the service should start the activity. Of course that
 doesn't sound very efficient at all, but in addition to this it
 doesn't work either. Thats my code from the service:

 public void onCreate() {
super.onCreate();
Intent i = new Intent(android.intent.action.MAIN);
ComponentName n = new
ComponentName(com.schwimmer.android.carmode,
com.schwimmer.android.carmode.ToggleCarMode);
i.setComponent(n);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
//this.stopSelf();

 If I put this code into an activity and start it wit
 this.startActivity() it works like a charm. But not if it is put into
 the receiver or server.

 Do you have any idea how to fix that?

 Thanks!
 Marc

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en




-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en