I’m trying to allow the user to choose which app to open when they press on my widget. It seems the way to do this is to use the ACTION_PICK_ACTIVITY and somehow save the value they’ve picked.
http://www.openintents.org/en/node/263 seems helpful, but does not show how to extract the data from the Intent in able to save it for future Intent creation.
How can I easily allow the user to pick from a list of applications installed on his or her phone and then save the chosen package/class for future intent creation?
View.OnClickListener activity_picker_listener = new View.OnClickListener(){
@Override
public void onClick(View v){
// Pick an application
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
startActivityForResult(pickIntent, 0);
}
};
// The result is obtained in onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
// save the application that we just picked
}
}
You could save the package name of the chosen application in your application (sdCard, cache, etc). Then, when the user clicks on your widget and calls the click listener, you do this:
UPDATE:
This will get all the installed applications on the user’s phone:
The ResolveInfo class contains a lot of useful information about the application, including its package name. Once the user chooses an application, you get the package name from its ResolveInfo object and use the other code to open the progam.