177

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.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Sathish Sathish
  • 2,251
  • 3
  • 20
  • 21
  • 2
    Possible duplicate of [Android - check for presence of another app](http://stackoverflow.com/questions/3694267/android-check-for-presence-of-another-app) – noelicus Apr 04 '17 at 09:19

17 Answers17

345

Try with this:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Add respective layout
        setContentView(R.layout.main_activity);

        // Use package name which we want to check
        boolean isAppInstalled = appInstalledOrNot("com.check.application");  
        
        if(isAppInstalled) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.check.application");
            startActivity(LaunchIntent);
                    
            Log.i("SampleLog", "Application is already installed.");          
        } else {
            // Do whatever we want to do if application not installed
            // For example, Redirect to play store

            Log.i("SampleLog", "Application is not currently installed.");
        }
    }

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

        return false;
    }

}
Community
  • 1
  • 1
Aerrow
  • 12,086
  • 10
  • 56
  • 90
  • 1
    @Sathish: I hope it may helpful for you – Aerrow Jul 09 '12 at 09:57
  • 2
    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) " – DJhon May 14 '14 at 06:16
  • 2
    @BlueGreen: Hi,hope this link may help you, http://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception, else if you are using Dialog Class means kindly check it. :) – Aerrow May 14 '14 at 06:19
  • 1
    @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? – DJhon May 14 '14 at 06:39
  • @BlueGreen: The above code will check only the installed app, at the moment of installation this will not work. For your case, use Broadcast receiver to get the notification after that apk installed. – Aerrow Aug 26 '14 at 04:54
  • Gotta hand it to Android to make you use exceptions for flow control. But thanks for the solution. – QED Aug 29 '15 at 22:10
  • What about to check that LinkedIn app is installed or not ? – Jaimin Modi Apr 26 '16 at 07:38
  • @JaiminModi: did you tried with this package name "com.linkedin.android" – Aerrow Jun 13 '16 at 05:57
  • does this require any specific Android permissions to work? – Bootstrapper Feb 22 '17 at 14:04
  • @Bootstrapper hope no need for additional permissions – Aerrow Feb 23 '17 at 07:34
  • I want to check self update. so I should check version – Mahdi Aug 13 '17 at 09:47
  • @Kenji - Sorry could you please explain in detail. – Aerrow Aug 13 '17 at 11:54
  • I want to install the same package from app ( update app) in case of force update I had to find-out apk is installed or not. – Mahdi Aug 13 '17 at 12:14
  • @Kenji - why you need to check if you check. This code will provide the result what ever package you have to check. reg update/force update you no need to check. – Aerrow Aug 14 '17 at 07:37
  • 1
    Got crash on `API 23` `Fatal Exception: java.lang.RuntimeException Package manager has died` – Mathi Arasan Aug 09 '18 at 06:31
  • why did you put `PackageManager.GET_ACTIVITIES`. it should `0` or `PackageManager.GET_ACTIVITIES` ? – Alireza Noorali Jan 22 '19 at 11:43
  • if anyone facing issue on Android 11 or up. Please follow guide in this answer `https://stackoverflow.com/a/70870680/15680126` – Ali Ahmed Jul 28 '22 at 12:25
77

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 = AndroidUtils.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.

Jonik
  • 80,077
  • 70
  • 264
  • 372
31

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;  
}
Aditya
  • 5,509
  • 4
  • 31
  • 51
Priyank Desai
  • 3,693
  • 1
  • 27
  • 17
18

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.isEmpty();
}
M. Prokhorov
  • 3,894
  • 25
  • 39
Kavi
  • 3,880
  • 2
  • 26
  • 23
12

Android 11 update
You have to specify in the manifest the exact bundle id's you want to search for.

Example for facebook and whatsapp:

Inside the Manifest above "application" (where the permissions are)

<queries>
    <package android:name="com.whatsapp" />
    <package android:name="com.facebook.katana" />
</queries>  

This will allow you to check if facebook and whatsapp are installed, otherwise you will always get false for that check.

Further reading on the subject:
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9

Mark Kazakov
  • 946
  • 12
  • 17
7

This code checks to make sure the app is installed, but also checks to make sure it's enabled.

private boolean isAppInstalled(String packageName) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return pm.getApplicationInfo(packageName, 0).enabled;
    }
    catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
caly__pso
  • 168
  • 3
  • 16
  • It's the package name of the app. For example, "com.example.apps.myapp". I have edited my answer to show packageName instead of uri. – caly__pso Jul 11 '16 at 14:30
6

Check App is installed or not in Android by using kotlin.

Creating kotlin extension.

fun PackageManager.isAppInstalled(packageName: String): Boolean = try {
        getApplicationInfo(packageName, PackageManager.GET_META_DATA)
        true
    } catch (e: Exception) {
        false
    }

Now, can check if app is install or not

if (packageManager.isAppInstalled("AppPackageName")) {
    // App is installed
}else{
    // App is not installed
}
Niral Dhameliya
  • 199
  • 3
  • 4
4

A simpler implementation using Kotlin

fun PackageManager.isAppInstalled(packageName: String): Boolean =
        getInstalledApplications(PackageManager.GET_META_DATA)
                .firstOrNull { it.packageName == packageName } != null

And call it like this (seeking for Spotify app):

packageManager.isAppInstalled("com.spotify.music")
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
  • As of Android 11, this method no longer returns information about all apps; see https://g.co/dev/packagevisibility for details – Mori Sep 28 '22 at 17:04
4

Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):

public static boolean isPackageExists(@NonNull final Context context, @NonNull final String targetPackage) {
    List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if (targetPackage.equals(packageInfo.packageName)) {
            return true;
        }
    }
    return false;
}
Alexander Savin
  • 1,952
  • 1
  • 15
  • 30
3

I think using try/catch pattern is not very well for performance. I advice to use this:

public static boolean appInstalledOrNot(Context context, String uri) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
    if (packageInfoList != null) {
        for (PackageInfo packageInfo : packageInfoList) {
            String packageName = packageInfo.packageName;
            if (packageName != null && packageName.equals(uri)) {
                return true;
            }
        }
    }
    return false;
}
Egemen Hamutçu
  • 1,602
  • 3
  • 22
  • 34
  • The above code in kotlin ``` private fun isAppInstalled(context: Context, uri: String): Boolean { val packageInfoList = context.packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES) return packageInfoList.asSequence().filter { it?.packageName == uri }.any() } ``` – Kishan B Sep 01 '17 at 12:42
1

Try this

This code is used to check weather your application with package name is installed or not if not then it will open playstore link of your app otherwise your installed app

String your_apppackagename="com.app.testing";
    PackageManager packageManager = getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = packageManager.getApplicationInfo(your_apppackagename, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (applicationInfo == null) {
        // not installed it will open your app directly on playstore
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + your_apppackagename)));
    } else {
        // Installed
        Intent LaunchIntent = packageManager.getLaunchIntentForPackage(your_apppackagename);
        startActivity( LaunchIntent );
    }
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
1

All the answers only check certain app is installed or not. But, as we all know an app can be installed but disabled by the user, unusable.

Therefore, this solution checks for both. i.e, installed AND enabled apps.

public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
     try {
          return packageManager.getApplicationInfo(packageName, 0).enabled;
     }
     catch (PackageManager.NameNotFoundException e) {
          return false;
     }
}

Call the method isPackageInstalled():

boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());

Now, use the boolean variable isAppInstalled and do whatever you want.

if(isAppInstalled ) {
    /* do whatever you want */
}
Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
1

In Kotlin, the simplest way can be two steps

1- in the Manifest put the target app id . ex (com.src.turkey)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <queries>
        <package android:name="com.src.turkey" />
               </queries>
...

2- In an Activity

 try {

 val list = packageManager.getLaunchIntentForPackage("com.src.turkey")
        if (list != null) {
            Log.i("TAG", "downloadApps:$list")
        }

    } catch (e: PackageManager.NameNotFoundException) {
        Log.i("TAG", "downloadApps: False")
    }

There isn't any deprecated such as

queryIntentActivities

pm.getPackageInfo
Mori
  • 2,653
  • 18
  • 24
  • These queries on AndroidManifest is a step that is forgotten by most answers, if you try, for example, the accepted answer without this on a device above Android 11 it won't work. – luiscosta May 25 '23 at 11:21
0

@Egemen Hamutçu s answer in kotlin B-)

    private fun isAppInstalled(context: Context, uri: String): Boolean {
        val packageInfoList = context.packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
        return packageInfoList.asSequence().filter { it?.packageName == uri }.any()
    }
Kishan B
  • 4,731
  • 1
  • 19
  • 11
0

A cool answer to other problems. If you do not want to differentiate "com.myapp.debug" and "com.myapp.release" for example !

public static boolean isAppInstalled(final Context context, final String packageName) {
    final List<ApplicationInfo> appsInfo = context.getPackageManager().getInstalledApplications(0);
    for (final ApplicationInfo appInfo : appsInfo) {
        if (appInfo.packageName.contains(packageName)) return true;
    }
    return false;
}
Nicolas Duponchel
  • 1,219
  • 10
  • 17
0

So nicer with Kotlin suger:

  private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {

    val packageManager = context.packageManager

    return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
  }
David
  • 2,129
  • 25
  • 34
0

You can do it using Kotlin extensions :

fun Context.getInstalledPackages(): List<String> {
    val packagesList = mutableListOf<String>()
    packageManager.getInstalledPackages(0).forEach {
        if ( it.applicationInfo.sourceDir.startsWith("/data/app/") && it.versionName != null)
            packagesList.add(it.packageName)
    }
    return packagesList
}

fun Context.isInDevice(packageName: String): Boolean {
    return getInstalledPackages().contains(packageName)
}
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73