Android - launch app from broadcast receiver

前端 未结 4 449
-上瘾入骨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: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

提交回复
热议问题