更新后如何自动启动Launcher应用?无需点击「始终」按钮
Hey there! This is a super common headache for launcher devs—let’s break down why this happens and what you can do about it.
Why the Prompt Shows Up
When your launcher app gets updated, Android temporarily removes its status as the default home app. This is a system security/UX measure to make sure users still have control over which app they use to navigate their device. So when you try to launch it post-update, the system pops up that dialog to confirm the default again.
Practical Fixes
Use the Right Broadcast Receiver
Instead of listening to android.intent.action.PACKAGE_REPLACED (which triggers for every app update), switch to android.intent.action.MY_PACKAGE_REPLACED. This broadcast is only sent to your app when it’s updated, so it’s more precise and doesn’t require extra system permissions.
Add this to your AndroidManifest.xml:
<receiver android:name=".UpdateReceiver"> <intent-filter> <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> </intent-filter> </receiver>
Add Critical Intent Flags
When launching your app from the receiver, include two key flags to help the system restore your app’s task stack correctly, which can sometimes bypass the prompt:
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); if (launchIntent != null) { launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); context.startActivity(launchIntent); }
FLAG_ACTIVITY_NEW_TASK ensures the app starts in a new task (required for launching from a broadcast receiver), and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED tells the system to reset the task stack to its default state, which helps re-establish your launcher’s home app context.
Check and Guide Default Launcher Setup
If the prompt still appears (which can happen on some Android versions or custom ROMs), you can check if your app is still the default launcher and guide the user to re-set it. This doesn’t eliminate the user’s click, but it makes the process clearer.
Add these helper methods to your receiver:
// Check if your app is the default launcher private boolean isDefaultLauncher(Context context) { PackageManager pm = context.getPackageManager(); Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME); ResolveInfo resolveInfo = pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY); return resolveInfo != null && context.getPackageName().equals(resolveInfo.activityInfo.packageName); } // Show a dialog to guide the user to settings private void showSetDefaultDialog(Context context) { new AlertDialog.Builder(context) .setTitle("Set as Default Launcher") .setMessage("To keep using our launcher, please select it and tap 'Always' in the settings.") .setPositiveButton("Go to Settings", (dialog, which) -> { Intent settingsIntent = new Intent(Settings.ACTION_HOME_SETTINGS); settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(settingsIntent); }) .setNegativeButton("Cancel", null) .show(); }
Call these in your receiver after launching the app:
if (!isDefaultLauncher(context)) { showSetDefaultDialog(context); }
Important Note
Unfortunately, there’s no way to completely bypass the user’s "Always" click for non-system apps. Android’s security model prevents apps from automatically setting themselves as default home apps—this is to protect users from unwanted launcher hijacking. The above methods will minimize the friction, but you’ll still need user confirmation in some cases.
内容的提问来源于stack exchange,提问作者Chirag Savsani

