eoinzy
eoinzy

Reputation: 2242

Android - How do I get package details for dynamic intents

So I am making a custom app chooser. It will have the browsers installed, and any map applications installed. I get each of these as follows:

PackageManager packageManager = activity.getPackageManager();      
//This gets all the browsers
String browserURI = "http://"+Driver.getIamhereURL()+lat+","+lon;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserURI));

List<ResolveInfo> browserList = packageManager.queryIntentActivities(browserIntent, 0);

// This gets all the Map apps:
String mapUri = "geo:"+lat+","+lon;
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mapUri));

List<ResolveInfo> mapList = packageManager.queryIntentActivities(mapIntent, 0);

//Combine the two
browserList.addAll(mapList);
//Now build layout and return a LinearLayout
LinearLayout appListBody = buildAppList(browserList,packageManager);

So appListBody will containt the app icon and app name hopefully. Obviously each app will have to have an onClickListener() associated with it, which will launch the intent. My problem is, how do I send the intent to my method, when all I can get is List<ResolveInfo> listOfApps?

I can try a for loop and go pm.getLaunchIntentForPackage(listOfApps.get(count).resolvePackageName) but then I get NullPointerException: package name is null.

Can anyone help?

Thanks.

Upvotes: 0

Views: 3340

Answers (2)

user2517419
user2517419

Reputation:

Using ResolveInfo if you want to get package name try this:

(ResolveInfo Object).activityInfo.packagename;

This will return a string of package Name.

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1007584

My problem is, how do I send the intent to my method, when all I can get is List listOfApps?

You can construct a suitable Intent yourself:

  @Override
  protected void onListItemClick(ListView l, View v,
                                 int position, long id) {
    ResolveInfo launchable=adapter.getItem(position);
    ActivityInfo activity=launchable.activityInfo;
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                         activity.name);
    Intent i=new Intent(Intent.ACTION_MAIN);

    i.addCategory(Intent.CATEGORY_LAUNCHER);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    i.setComponent(name);

    startActivity(i);     
  }

This is from my Launchalot sample project, which has a ListView on an adapter full of ResolveInfo objects.

I would think that your solution would be equivalent if not superior, but you will need to use activity.applicationInfo.packageName to get the package to use. resolvePackageName is not what you think it is. :-)

Upvotes: 3

Related Questions