How to make a custom button to launch applications?

I am trying to add a button to my application. When clicked, I would like to launch a selection dialog that shows all the shortcuts or installed applications. Once selected, you must permanently install the button to launch this application.

I understand how to use packagemanager to get a list of installed applications:

PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

but do you really need to accept this and use the ListAdapter and create a separate dialog from scratch?

It seems to me that I saw this selection menu in other applications several times (for example, any launch application when you add a shortcut or to the Google Car Home application when adding a new shortcut). Is there any way to use this context menu?

I searched these forums everywhere and cannot find otherwise. Any help would be much appreciated. Thank.

+3
source share
2 answers

but do you really need to accept this and use the ListAdapter and create a separate dialog from scratch?

To select an application, yes.

Is there any way to use this context menu?

This "selection context menu" does not select the application. He chooses an action, possibly using ACTION_PICK_ACTIVITY.

+1
source

For those who are interested, here is how I ended up doing this:

MainIntent Intent ( ) ACTION_MAIN addCategory CATEGORY_LAUNCHER, EXTRA pickIntent. , .

:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //rename R.id.plusbutton to match up with your button in xml
    Button plusButton = (Button)findViewById(R.id.plusbutton);

    plusButton.setOnClickListener(new View.OnClickListener() {          

    @Override
    public void onClick(View view) {
        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);   
        int requestCode = 1;
        //rename Main to your class or activity
        Main.this.startActivityForResult(pickIntent, requestCode);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (intent != null)
        startActivity(intent);
}
+1

All Articles