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

后端 未结 15 1293
悲哀的现实
悲哀的现实 2020-11-22 05:51

We have installed applications programmatically.

  1. If the application is already installed in the device the application is open automatically.
  2. Otherwis
15条回答
  •  暖寄归人
    2020-11-22 06:47

    You can do it using Kotlin extensions :

    fun Context.getInstalledPackages(): List {
        val packagesList = mutableListOf()
        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)
    }
    

提交回复
热议问题