Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android: choose default launcher programmatically

I want to pop up a dialog that lets the user choose a launcher to be launched with set as default option. I tried

Intent home = new Intent(Intent.ACTION_DEFAULT);
home.addCategory(Intent.CATEGORY_LAUNCHER);
Intent chooser = Intent.createChooser(home, "Launcher");
context.startActivity(chooser);

But the dialog popped by this does not have the option to set default. While the following code will not pop up the dialog if a default launcher is already set.

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

How can this be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);            
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...