Sie sind auf Seite 1von 7

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

76

How to check programmatically if an application is installed or not in Android?


android

apk

We have installed applications programmatically.


1. If the application is already installed in the device the application is open automatically.
2. Otherwise install the particular application.
Guide Me. I have no idea. Thanks.

share

improve this question


Sathish Sathish
636 2 11 18
Jonik
34.1k 39 169 229

5 Answers

180

Try with this:

Asked
Jul 9 '12 at 9:19

Edited
Jan 27 '15 at 16:18

Order By

Votes

startActivity(LaunchIntent);

share

System.out.println("App is already installed on your phone");


} else {
System.out.println("App is not currently installed on your phone");
}

private boolean appInstalledOrNot(String uri) {


PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}

improve this answer


Aerrow
6,834 8 31 70

Answered
Jul 9 '12 at 9:25

David Passmore
3,867 2 26 49

Edited
Mar 31 '15 at 18:55

@Sathish: I hope it may helpful for you Aerrow Jul 9 '12 at 9:57

Thanks @Aerrow. Sathish Sathish Jul 9 '12 at 10:16

No doubt your post is really helpful , but i am getting a exception "java.lang.RuntimeException: Adding window failed" and " E/AndroidRuntime(7554): Caused by:
android.os.TransactionTooLargeException 05-14 11:37:25.305: E/AndroidRuntime(7554): at android.os.BinderProxy.transact(Native Method) 05-14 11:37:25.305:
E/AndroidRuntime(7554): at android.view.IWindowSession$Stub$Proxy.add(IWindowSession.java:516) 05-14 11:37:25.305: E/AndroidRuntime(7554): at
android.view.ViewRootImpl.setView(ViewRootImpl.java:494) " Dilshad May 14 '14 at 6:16

@BlueGreen: Hi,hope this link may help you, stackoverflow.com/questions/11451393/, else if you are using Dialog Class means kindly check it. :) Aerrow May 14 '14
at 6:19

@Aerrow.. Suppose i am checking my .apk is installed or nor ? at time of installation... I am getting same exception while checking my package
com.test.installedornot.My .apk size is more than 9MB then in that case how i will manage this exception? Dilshad May 14 '14 at 6:39
show 3 more comments

19

The above code didn't work for me. The following approach worked.
Create an Intent object with appropriate info and then check if the Intent is callable or not using the following function:
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}

share

improve this answer


Priyank Desai
631 5 9
adi
3,363 3 18 40

This is better because it doesn't require using exceptions for flow control! QED Aug 29 '15 at 22:11

Answered
Jul 10 '13 at 3:10

Edited
Aug 29 '13 at 20:55

@QED it's appalling the amount of people using Exceptions as if statements! This is definitely the right answer AlanChavez Sep 7 '15 at 19:44

what's the content of the Intent? the String with the packageName doesn't work Henrique de Sousa Nov 18 '15 at 17:42

@HenriquedeSousa Intent intent = getPackageManager().getLaunchIntentForPackage("org.package.name"); DMan Jan 5 at 21:41


add a comment

14

Somewhat cleaner solution than the accepted answer (based on this question):
public static boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}

I chose to put it in a helper class as a static utility. Usage example:


boolean whatsappFound = AndroidHelper.isAppInstalled(context, "com.whatsapp");

This answer shows how to get the app from the Play Store if the app is missing, though care needs to be taken on devices that don't
have the Play Store.

share

improve this answer


Jonik
34.1k 39 169 229

Answered
Jan 27 '15 at 16:27

Edited
Jun 10 '15 at 8:28

If you know the package name, then this works without using a try-catch block or iterating through a bunch of packages:
public static boolean isPackageInstalled(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
if (intent == null) {
return false;
}
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}

share

improve this answer


Kavi
2,339 2 12 18

This code checks to make sure the app is installed, but also checks to make sure it's enabled.
private boolean isAppInstalled(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
if(this.getPackageManager().getApplicationInfo(uri, 0).enabled) {
return true;
} else {
return false;
}

share

}
catch (PackageManager.NameNotFoundException e) {
return false;
}

improve this answer

Answered
Jun 8 '15 at 11:41

youravgjoe
13 6

Answered
20 hours ago

Your Answer

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Das könnte Ihnen auch gefallen