Android: Choose Default Launcher Programmatically

How to set default app launcher programmatically?

This is actually possible with a little workaround:

Create an empty Activity that acts as a launcher called FakeLauncherActivity. Add it to your manifest as a disabled component:

<activity
android:name="com.path.to.your.FakeLauncherActivity"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Check whether your desired launcher activity is the default one (with the isMyAppLauncherDefault() from your question).

If not, offer the user to choose the preferred launcher activity like this:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, com.path.to.your.FakeLauncherActivity.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);
}

This method temporarily enables FakeLauncherActivity, which leads to a change in the set of available launcher activities, which forces Android to forget its default launcher. You will see something like...

521-735/system_process I/PackageManager﹕ Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 } type null

... in your log.

The method then simply opens a launcher intent where you can see all installed launchers and the buttons "Always" / "Just once".
Finally, the method disables FakeLauncherActivity again so that it doesn't display in the list.

You could repeat that as often as you want and only let the user proceed if your desired launcher activity is set as default.

android: choose default launcher programmatically

Try using the following:

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

If a default action is already set (yours), you can call first:

getPackageManager().clearPackagePreferredActivities(getPackageName());

If the default action is not yours, you cannot clear it programmatically, what you can do is to check if other app is set as default and show a message..

private boolean isMyLauncherDefault() {
PackageManager localPackageManager = getPackageManager();
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
String str = localPackageManager.resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName;
return str.equals(getPackageName());
}

As a workaround in case of other app is set as default, you can created a fake home, install it (this will force the system to clear the default app) and then uninstall it...

Manifest.xml

<activity
android:name="FakeHome" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

FakeHome.java

public class FakeHome extends Activity {

}

Somewhere

if (!isMyLauncherDefault()) {           
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(getApplicationContext(), FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
startActivity(selector);

p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

How to set Default launcher from Setting programatically?

I was search in Google ,StackOverFlow & Github but nothing I found,
by my luck Some Luncher help me to redirect HomeStting Activity in Levt & MI phones, it's work as per my expectation .
In Some it's work perfect ,I just Decompile it and Get used code for it.

for set Default launcher

new SetDefaultLauncher(activity).launchHomeOrClearDefaultsDialog();

.

package com.android.launcher;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.SpannableString;
import android.text.style.TtsSpan.TextBuilder;
import com.example.launcher.R;
import com.launcher2.activitys.FakeLauncher;

public class SetDefaultLauncher {

public static final String LAUNCHER_CLASS = "com.android.launcher.launcher3.Launcher";
public static final String LAUNCHER_PACKAGE = "com.android.launcher";

Activity activity;
SetDefaultLauncher(Activity activity){
this.activity=activity;
}
enum HomeState {
GEL_IS_DEFAULT, OTHER_LAUNCHER_IS_DEFAULT, NO_DEFAULT
}
public boolean launchHomeOrClearDefaultsDialog() {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
ResolveInfo resolveActivity = activity.getPackageManager().resolveActivity(
intent, 0);
HomeState homeState = (LAUNCHER_PACKAGE
.equals(resolveActivity.activityInfo.applicationInfo.packageName) && LAUNCHER_CLASS
.equals(resolveActivity.activityInfo.name)) ? HomeState.GEL_IS_DEFAULT
: (resolveActivity == null
|| resolveActivity.activityInfo == null || !inResolveInfoList(
resolveActivity, activity.getPackageManager()
.queryIntentActivities(intent, 0))) ? HomeState.NO_DEFAULT
: HomeState.OTHER_LAUNCHER_IS_DEFAULT;
switch (homeState) {
case GEL_IS_DEFAULT:
case NO_DEFAULT:
intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
intent.setFlags(268435456);
activity.startActivity(intent);
return true;
default:
showClearDefaultsDialog(resolveActivity);
return false;
}
}
@SuppressLint("NewApi") private void showClearDefaultsDialog(ResolveInfo resolveInfo) {
CharSequence string;
final Intent intent;
CharSequence loadLabel = resolveInfo.loadLabel(activity.getPackageManager());
if (VERSION.SDK_INT < 21
|| activity.getPackageManager().resolveActivity(
new Intent("android.settings.HOME_SETTINGS"), 0) == null) {
string = activity.getString(R.string.change_default_home_dialog_body,
new Object[] { loadLabel });
intent = new Intent(
"android.settings.APPLICATION_DETAILS_SETTINGS",
Uri.fromParts("package",
resolveInfo.activityInfo.packageName, null));
} else {
intent = new Intent("android.settings.HOME_SETTINGS");
string = new SpannableString(activity.getString(
R.string.change_default_home_dialog_body_settings,
new Object[] { loadLabel }));
((SpannableString) string)
.setSpan(
new TextBuilder(
activity.getString(
R.string.change_default_home_dialog_body_settings_tts,
loadLabel)).build(), 0, string
.length(), 18);
}

new AlertDialog.Builder(activity)
.setIcon(R.drawable.ic_launcher)
.setMessage(string)
.setNegativeButton(
activity.getString(R.string.change_default_home_dialog_cancel),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
activity.finish();
}
})
.setOnCancelListener(new DialogInterface. OnCancelListener() {

@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
activity.finish();
}
})
.setPositiveButton(
activity.getString(R.string.change_default_home_dialog_proceed),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
try {
intent.setFlags(276856832);
activity.startActivity(intent);
} catch (Exception e) {
setDefLauncher(activity);
}
}
}).create().show();
}

private boolean inResolveInfoList(ResolveInfo resolveInfo,
List<ResolveInfo> list) {
for (ResolveInfo resolveInfo2 : list) {
if (resolveInfo2.activityInfo.name
.equals(resolveInfo.activityInfo.name)
&& resolveInfo2.activityInfo.packageName
.equals(resolveInfo.activityInfo.packageName)) {
return true;
}
}
return false;
}

private void setDefLauncher(Context c) {
PackageManager p = c.getPackageManager();
ComponentName cN = new ComponentName(c, FakeLauncher.class);
p.setComponentEnabledSetting(cN,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
c.startActivity(selector);
p.setComponentEnabledSetting(cN,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}

Launching default android launcher programmatically

Have you tried this?

startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER));

(I haven't tried it myself, because my use case is a little more complicated---I've replaced the launcher, and I want to call the old launcher...)

I've also discovered that you can use the package manager to look through all activities that meet some intent filter criteria. For example, if you want to find all the activities marked as the home default home activity, use this:

Intent intent=null;
final PackageManager packageManager=getPackageManager();
for(final ResolveInfo resolveInfo:packageManager.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY))
{
if(!getPackageName().equals(resolveInfo.activityInfo.packageName)) //if this activity is not in our activity (in other words, it's another default home screen)
{
intent=packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName));
break;
}
}

Note that I have replaced the default home screen on my device---that's why I have to make sure the activity I found is not the activity that's running! If you haven't replaced the default home activity, you don't need this check---just use the first (and probably the only) default home activity.

(Note that I still can't launch the old launcher from my launcher, perhaps because the old launcher keeps a record of the default launcher, which is my new launcher, and simply calls back to it. I don't know. But at least it doesn't crash, and I would guess that, if you haven't replaced the old home screen, it just might work.)

How do I programmatically set the default launcher app?

This is not possible, except perhaps via some security flaw. I will look into it and try to get this flaw fixed, assuming that this app actually works.

Rooted devices should be able to do this, but not unrooted ones.


UPDATE

These apps do not actually set the default home, near as I can tell. They are simply calling startActivity() with a MAIN/LAUNCHER Intent, using createChooser() to force a chooser dialog to appear, giving the user the opportunity to choose a home screen and make it the default. This is perfectly legitimate, as it requires user involvement in the process.

Leastways, three of them work this way, while one simply crashes when trying to set a home screen.

flutter: choose launcher as default programmatically

in case anyone needs the answer to this question, I did it using the intent package and by the following code:

void displayDefaultLauncherChooser() {
intentFlutter
.Intent()
..setAction(actionFlutter.Action.ACTION_MAIN)
..addCategory("android.intent.category.HOME")
..addCategory("android.intent.category.DEFAULT")
..addFlag(flag.Flag.FLAG_ACTIVITY_NEW_DOCUMENT)
..startActivity().catchError((e) => print("intent error: " + e.toString()));
}

Call select launcher dialog when my app isn't the default launcher app

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.



Related Topics



Leave a reply



Submit