Cistoran
Cistoran

Reputation: 1597

Get And Display List Of Installed Programs on Android

I'm currently developing an app that will allow the user to choose an app and launch it at a later time (there is more functionality but this is the main thing I'm having an issue with.)

I'm looking for a way to get a list of the applications (user installed or updateable ex. Gmail, GMaps, etc...) And throw it into an AlertDialog similar to how you add a shortcut to the Homescreen (Long press -> Applications).

This is the thread I'm using that has the code to get the list of applications that I need. However how would I turn this into an AlertDialog?

Here is the code from the thread.

public void getApps()
{
    PackageManager pm = getPackageManager();
    List<ApplicationInfo> apps = pm.getInstalledApplications(0);
    List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();

    for(ApplicationInfo app : apps) {
        //checks for flags; if flagged, check if updated system app
        if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
            installedApps.add(app);
        //it's a system app, not interested
        } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            //Discard this one
        //in this case, it should be a user-installed app
        } else {
            installedApps.add(app);
        }
    }
}//end getApps()

And here is the code I use for displaying an AlertDialog similar to what I want to use.

//PseudoCode does not compile
public void displayAppList(View v)
{
    final CharSequence[] items = {getApps()};

    AlertDialog.Builder builder = new AlertDialog.Builder(SchedulerActivity.this);
    builder.setTitle("Choose an App To Launch");
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            appChoiceString[count] = items[item];
     Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    });

    builder.setPositiveButton("Yes",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
       Toast.makeText(SchedulerActivity.this, "Success", Toast.LENGTH_SHORT).show();
      }
     });
    builder.setNegativeButton("No",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
       Toast.makeText(SchedulerActivity.this, "Fail", Toast.LENGTH_SHORT).show();
      }
     });
    AlertDialog alert = builder.create();
    alert.show();
}

Any help as to getting this to display properly would be awesome.

Upvotes: 0

Views: 1499

Answers (1)

kabuko
kabuko

Reputation: 36302

Why not just use the standard intent chooser? (See this). Otherwise, you probably want to explain what is not displaying the way you want, and how you really want it to look in detail.

Upvotes: 3

Related Questions