2

I want to start another application's Activity from my Activity, but all info, that I have is only the name of that application's package. I do following:

Intent intent = new Intent();
intent.setPackage(anotherApplicationPackageName);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);

As I understand correctly, using this API, Android will look for an Activity, which can handle this Intent inside the given package, but I always get ActivityNotFoundException.

The question is What am I doing wrong?

teoREtik
  • 7,886
  • 15
  • 46
  • 65
  • Find the answer to my question in [this][1] post [1]: http://stackoverflow.com/questions/3872063/android-launch-an-application-from-another-application – teoREtik Oct 27 '11 at 11:32

4 Answers4

8
    PackageManager  pmi = getPackageManager();
    Intent intent = null;

intent = pmi.getLaunchIntentForPackage(packageNameToLaunch);                    
if (intent != null){
    startActivity(intent);
}
user462990
  • 5,472
  • 3
  • 33
  • 35
1

As you know the package name of the other app and can't add any intent filter to the destination app, then you should use this to launch the other app,

Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(anotherApplicationPackageName);

startActivity(intent);

It works fine.

Arup Saha
  • 121
  • 1
  • 2
1

You have to declare another activity's intent filter with category 'Intent.CATEGORY_DEFAULT' and create a custom action and call it using that category and action, setting the package in the intent only reduces its scope.

Giohji
  • 714
  • 7
  • 12
  • 1
    I can't make this, because application I try to lunch is not my, and why did you decide that I should decalre another one Intent Filter in another application's activity, where does it described? – teoREtik Oct 27 '11 at 11:27
  • Then you should try launching the activity with: `intent.setComponent (ComponentName component)` – Giohji Oct 27 '11 at 11:37
  • `setComponent` also assumes using activity name, I have solved this problem, look at the comments to my question, thanks for your respond)) – teoREtik Oct 27 '11 at 11:44
  • Thanks and +1 for the hint regarding CATEGORY_DEFAULT. – Ridcully May 19 '12 at 08:20
0

I had a similar problem for Intent.ACTION_SEND. Probably this code will be useful for someone:

/** Knowing intent and package name, set the component name */
public static Intent setComponentByPackageName(Context context, String packageName, Intent intent0) {
    Intent intent = new Intent(intent0);
    if (null == intent.getType()) {
        intent.setType("text/plain");
    }
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> rList = pm.queryIntentActivities(intent, 0);
        // possible flags:
        // PackageManager.MATCH_DEFAULT_ONLY  PackageManager.GET_RESOLVED_FILTER
    for (ResolveInfo ri : rList) {
        if (packageName.equals(ri.activityInfo.packageName)) {
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            return intent;
        }
    }
    return null;
}

In the course of trying to make it work (the ResolveInfo list was empty), I found out that a setType() was missing (you don't need to setType() and setPackage() if you setComponent()), so in fact the following is enough:

    intent.setAction(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.setPackage("com.xxxxxx");
    // ...
    intent.putExtra(Intent.EXTRA_TEXT, text);
    // ...
    startActivityForResult(intent, ...);
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127