android: choose default launcher programmatically

后端 未结 3 487
栀梦
栀梦 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: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
            }
        }
    }
    

提交回复
热议问题