Android - launch app from broadcast receiver

前端 未结 4 443
-上瘾入骨i
-上瘾入骨i 2021-01-12 00:08

I want the broadcast receiver to launch my app this way:

  1. If the app is not in the foreground or background, launch it as if it is launched from launcher.

相关标签:
4条回答
  • 2021-01-12 00:41

    Assuming you already know BroadcastReceiver, you just have to do the launching like you usually do on onReceive : Therefore it will look like this

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent launch_intent = new Intent("android.intent.action.MAIN");
        launch_intent.putExtra("some_data", "value");
        tiapp.startActivity(launch_intent);
    }
    

    If it is launched before, it will just resume that Activity, if not, it will start a new

    0 讨论(0)
  • 2021-01-12 00:49

    Since you need behaviour like

    1) If the app is not in the foreground or background, launch it as if it is launched from the launcher.

    2) If the app is in the background, bring it to the foreground with the state it was last in.

    3) If the app is in the foreground, do nothing.

    Consider:

    @Override
    public void onReceive(Context context, Intent intent) {
    
        Intent startIntent = context
                .getPackageManager()
                .getLaunchIntentForPackage(context.getPackageName());
    
        startIntent.setFlags(
                        Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
                        Intent.FLAG_ACTIVITY_NEW_TASK |
                        Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
        );
        context.startActivity(startIntent);
    }
    
    0 讨论(0)
  • 2021-01-12 00:51

    You don't need to write all that code. You can use a "launch Intent", as follows:

    PackageManager pm = context.getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage("com.example.helloworld");
    launchIntent.putExtra("some_data", "value");
    context.startActivity(launchIntent);
    

    However, your code should also work.

    If you say this works most of the time, you might be seeing a nasty old Android bug in those cases. This occurs when you launch your app for the first time either directly from the installer, or via an IDE (Eclipse, Android Studio, etc.). You should always launch your app for the first time directly from the HOME screen or list of installed apps. For more information about this frustrating Android bug see https://stackoverflow.com/a/16447508/769265

    0 讨论(0)
  • 2021-01-12 00:51
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent launch_intent = new  Intent(context, MainActivity.class);
        launch_intent.setComponent(new ComponentName("com.example.helloworld","com.example.helloworld.MainActivity");
        launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |INTENT.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        launch_intent.putExtra("some_data", "value");
        launch_intent.addCategory(Intent.CATEGORY_LAUNCHER);
        context.startActivity(launch_intent);
    }
    

    It's this simple but unless you know the proper way when you try to get the extra data from your intent you will usually get a java null pointer exception.

    Use the onNewIntent() method to receive the extra data. If you want to know how comment below.

    Edit: Try the following. But what I don't understand is the first code which I gave you should work on its own. It has always worked for me. Have you made sure that you have copied the first code I gave you and pasted it without making any changes?

    Edit2: If that doesn't work try setting the flag to clearTaskOnLaunch instead of INTENT.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

    0 讨论(0)
提交回复
热议问题