Reputation: 2271
We have installed applications programmatically.
Guide Me. I have no idea. Thanks.
Upvotes: 179
Views: 205814
Reputation: 4611
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
Upvotes: 2
Reputation: 1116
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
Upvotes: 17
Reputation: 12134
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;
}
}
Upvotes: 346
Reputation: 3971
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)
}
Upvotes: 1
Reputation: 2169
So nicer with Kotlin suger:
private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {
val packageManager = context.packageManager
return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
}
Upvotes: 0
Reputation: 219
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
}
Upvotes: 6
Reputation: 1349
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;
}
Upvotes: 0
Reputation: 2879
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 */
}
Upvotes: 1
Reputation: 2012
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;
}
Upvotes: 4
Reputation: 8293
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")
Upvotes: 4
Reputation: 3880
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();
}
Upvotes: 18
Reputation: 3793
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 );
}
Upvotes: 1
Reputation: 5325
@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()
}
Upvotes: 0
Reputation: 81751
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.
Upvotes: 77
Reputation: 1702
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;
}
Upvotes: 3
Reputation: 168
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;
}
}
Upvotes: 7
Reputation: 3835
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;
}
Upvotes: 31