0

I am searching for a possibility to call 3rd party apps by using the PackageManager. The major problem is, that my method callApp implements the interface of another solution, which is not accessable. I only recieve the parameter appName without package-information.

When the Intent is initialized, i have to add my packagepath (here: com.example) by hand.

I want to achieve, that Android finds the full name of the package, which matches to that name. Because this method is part of another project, i can not use getApplicationContext().getPackageName(); or similar. Any ideas?

public void callApp(int methodIndicator, String appName, String command, Map<String, String> args) {
    PackageManager pm = context.getPackageManager();

    // < Should only be the name
    Intent launchIntent = pm.getLaunchIntentForPackage("com.example."+appName);   
    launchIntent.putExtra("method", methodIndicator);
    launchIntent.putExtra("command", command);
    launchIntent.putExtra("args", new HashMap<String, String>(args));
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    context.startActivity(launchIntent);
}
Gary Klasen
  • 1,001
  • 1
  • 12
  • 30

1 Answers1

1

In order to get a list of the installed apps on a device, you may use the following code:

PackageManager pm = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pgList = pm.queryIntentActivities(mainIntent, 0);

for (ResolveInfo rInfo : pgList) {
    // do your stuff
}

The ResolveInfo object gives access to both name and package through the following accessors:

rInfo.activityInfo.packageName // the application package
rInfo.activityInfo.loadLabel(pm).toString() // the application name as displayed in launcher

From there you may look for your appName and get the related package.

Hope that helps.

malrok44
  • 592
  • 6
  • 17