Pergunta

I am trying to add a button to my application. When clicked I would like to launch a selection dialog that shows all shortcuts or installed applications. Choosing one should permanently set the button to launch that 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 I really need to take this and use a ListAdapter and create a seperate dialog from scratch?

I feel like I've seen this selection menu in other apps multiple times (such as any launcher app when you go to add a shortcut, or in Google's Car Home app when you add a new shortcut). Is there no stock way to use this shortcut selection menu?

I've searched these forums all over and can't find otherwise. Any help will be much appreciated. Thank you.

Foi útil?

Solução

but do I really need to take this and use a ListAdapter and create a seperate dialog from scratch?

For picking an application, yes.

Is there no stock way to use this shortcut selection menu?

That "shortcut selection menu" is not picking an application. It is picking an activity, probably using ACTION_PICK_ACTIVITY.

Outras dicas

For those interested, here is how I eventually accomplished it:

When you create the Intent mainIntent (in the code below) and use ACTION_MAIN and addCategory CATEGORY_LAUNCHER, you can add it as an EXTRA for pickIntent. Doing this narrows down the picker menu to show only installed applications.

Here is some code to get a simple launcher button going:

@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);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top