Tuesday, 30 October 2018

How can I listen to which app has been chosen as Launcher Application?

I tried to implement a functionality that allows user to pick default Android's default Launcher Application. Also, I need to receive information which application has been chosen. But there is a problem with that approach.

To let user pick Launcher Application, we can simply start given intent:

val selector = Intent(Intent.ACTION_MAIN)
selector.addCategory(Intent.CATEGORY_HOME)
selector.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(selector)

It results in such dialog:

enter image description here

What I observed, if I use startActivity, the Launcher Application is set nicely and works as intended, but if I use startActivityForResult, then I will get some callback, but the Launcher Application will not be set at all. Also, there was nothing interesting in intent received in onActivityResult.

Then, I tried using IntentSender instead.

The code looks as follows:

val selector = Intent(Intent.ACTION_MAIN)
selector.addCategory(Intent.CATEGORY_HOME)
selector.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val receiver = Intent(this, MyBroadcastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT)
val chooser = Intent.createChooser(selector, "Select a Home app", pendingIntent.intentSender);
startActivity(chooser)

The receiver looks as follows:

class MyBroadcastReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val componentName = intent.extras.getParcelable<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT)
        //com.example.myapp if my app was choosen
        val pkg = componentName.packageName
    }
}

This results in default chooser, without options "JUST ONCE" or "ALWAYS". I don't have exact picture, but it looks similar to this one:

enter image description here

This works in a way, in the receiver's onReceive method I get ComponenName object, that holds selected app packageName. The problem is - again - Launcher Application is not set!

So question is: How can I let user set Launcher Application, and also receive information which one has he chosen?



from How can I listen to which app has been chosen as Launcher Application?

No comments:

Post a Comment