简体   繁体   中英

ANDROID - Launch other application from a BroadcastReceiver

I need to launch/open one installed apk in my device from a BroadcastReceiver.

Here is the code:

public class C2DMMessageReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    Log.w("C2DM", "Message Receiver called");
    if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
        Log.w("C2DM", "Received message");
        ComponentName toLaunch = new ComponentName("es.mypackage","es.mypackage.myapplication");
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER); 
        intent.setComponent(toLaunch); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);

My device receives the broadcast but fails with an unexpected problem.

The code to launch other apk works fine in other part of the application.

Is possible to launch other application from a broadcast?

Thank you very much.

As per my experience, you can not start activity from the C2DM Receiver, I found work around for that, Create one service and start activity from that service, stop service after you start the activity.

Thank You,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM