7

I am wondering how to start an another app from my app using package name.

I have package name in string format like

String pkgName = "com.example.appName";

That is why i am unable to fetch class name or any other valuable details from it which i can use to make a proper intent to start an activity.

Any idea how to solve this problem. Please Help!!

Thanks.

Varundroid
  • 9,135
  • 14
  • 63
  • 93

1 Answers1

17

well, you don't want to fetch ANY class name. What you want to do is to create an Intent out of this package name with Activity's that can be launched (they need to have specific category). In a single line, what you need to do is:

startActivity(getPackageManager().getLaunchIntentForPackage("com.example.appName"));

Check the documentation for PackageManager.getLaunchIntentForPackage. Generally speaking, PackageManager has a lot of interesting methods for launching apps. Here is my blog entry doing exactly that for multiple packages.

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • Hey thanks for solving this mystery for me. but when i am trying your solution its giving me NullPointerException. any idea why? – Varundroid Apr 30 '11 at 09:47
  • very nice blog. and i found why it was showing me nullpointerexception because i was trying to open google search widget :P ..well thanks for your help. :) – Varundroid Apr 30 '11 at 09:55
  • hehe, i didn't even make it to look into that NullPointerException and you solved it. – Ilya Saunkin Apr 30 '11 at 11:06
  • 1
    @ElijahSaounkine i have successfully started another activity but i want to close my whole app – Erum Feb 06 '15 at 06:03
  • @ElijahSaounkine java.lang.IllegalStateException: Could not execute method of the activity – Dr.jacky Sep 08 '16 at 07:39
  • don't do it in a single line if package doesn't found . intent will be null – Zar E Ahmer Nov 17 '16 at 06:03