android: choose default launcher programmatically

后端 未结 3 444
栀梦
栀梦 2020-12-02 16:25

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(         


        
相关标签:
3条回答
  • 2020-12-02 16:42

    Try this :

        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        getContext().startActivity(intent);
    
    0 讨论(0)
  • 2020-12-02 16:51

    Starting from API 29 this is now officially supported using RoleManager.

    A very simple Kotlin example of a method you can call from any activity:

    fun showLauncherSelector(activity: AppCompatActivity, requestCode : Int) {
        val roleManager = activity.getSystemService(Context.ROLE_SERVICE) as RoleManager
        if(roleManager.isRoleAvailable(RoleManager.ROLE_HOME)){
            val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_HOME)
            activity.startActivityForResult(intent, requestCode)
        }
    }
    

    Then you can check for errors in the caller activity:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == MY_REQUEST_CODE) {
            if (resultCode != Activity.RESULT_OK) {
                // Something went wrong
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 16:52

    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);            
    }
    
    0 讨论(0)
提交回复
热议问题