Reputation: 85
I'm doing an app that is the default launcher of my device, after a lot of searches I found that isn't possible define programmatically my app as the default launcher. So I decided to do the following, I will enable to an admin user set this app as the default and this admin is going to be the person who will install the app and configure it at first time, before giving the device to another user. The only way to get out of app is pressing the back button, when user press it a dialog will appear asking for a password, if password typed is the right one the app will be removed as the default launcher
I used this line code to do this:
getPackageManager().clearPackagePreferredActivities(getPackageName());
This works fine and I can back to home screen of device normally, what i want is that, when an user got out of app and back to it, i want to verify if the app is the default launcher, and if it is not, show the "select launcher" dialog for choose it as default.
I've tried all this options:
Android home-screen/launcher chooser does not show 'use by default for this action' option
android: choose default launcher programatically
How to set default app launcher programmatically?
But any of this worked, it can't recognize that my app isn't more the default launcher. But when i press the home button the dialog to select launcher appears. I want when start my app again this dialog appear, without i press the home button.
this is the method i'm using to find if is my default launcher:
private boolean isMyLauncherDefault() {
final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
List<IntentFilter> filters = new ArrayList<>();
filters.add(filter);
final String myPackageName = getPackageName();
List<ComponentName> activities = new ArrayList<>();
PackageManager packageManager = getPackageManager();
// You can use name of your package here as third argument
packageManager.getPreferredActivities(filters, activities, null);
if(activities.size() == 0) //no default
return true;
for (ComponentName activity : activities) {
if (myPackageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}
When i look at activities array on debug my app is still there, but how i said, android know that my app isn't more the default. i need this method recognize this too.
And the following method suppose to call the dialog again:
public static void resetPreferredLauncherAndOpenChooser(Context context) {
PackageManager packageManager = context.getPackageManager();
packageManager.clearPackagePreferredActivities(context.getPackageName());
ComponentName componentName = new ComponentName(context, VideoActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selector);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}
All this methods came from previous links.
I will be very grateful if someone can help me.
PS: I'm using the sdk target version 21
Upvotes: 0
Views: 2472
Reputation: 85
I Found the solution. I used this answer: https://stackoverflow.com/a/7824190/2382100
And my code look like this:
ComponentName componentName = new ComponentName(this, VideoActivity.class);
getPackageManager().setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, PackageManager.DONT_KILL_APP);
This will disable my app , but if i understand well , it still going to be the default launcher but disabled, and it will back to home screen, after back to home screen, if user open it again, then the code will do this:
Log.d(TAG, "Recreating launcher");
PackageManager packageManager = context.getPackageManager();
packageManager.clearPackagePreferredActivities(context.getPackageName());
ComponentName componentName = new ComponentName(context,VideoActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
Works very well, glad of Palani's answer.
Upvotes: 1