How to Check Programmatically If an Application Is Installed or Not in Android

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

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;
}

}

How to know if a particular application is installed or not on the device?

I found the answer here

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList =
context.getPackageManager().queryIntentActivities( mainIntent, 0);
for (ResolveInfo resolveInfo : pkgAppsList) {
Log.d(TAG, "__<>"+resolveInfo.activityInfo.packageName);
if (resolveInfo.activityInfo.packageName != null
&& resolveInfo.activityInfo.packageName.equals(uri)) {
return true;
}
}
return false;

This worked for me perfectly.

Check if application is installed - Android

Try this:

private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.

Note that we pass in a PackageManager instead of a Context, so that the method is slightly more flexibly usable and doesn't violate the law of Demeter. You can use the method without access to a Context instance, as long as you have a PackageManager instance.

Use it like this:

public void someMethod() {
// ...

PackageManager pm = context.getPackageManager();
boolean isInstalled = isPackageInstalled("com.somepackage.name", pm);

// ...
}

Note: From Android 11 (API 30), you might need to declare <queries> in your manifest, depending on what package you're looking for. Check out the docs for more info.

How can I check whether an app is installed and open it?

Use this method.

public void OpenApp() {
PackageManager pm = getPackageManager();
final String LiveAppPackage = "com.example.app"; //Change to your package name.
try {
pm.getPackageInfo(LiveAppPackage, PackageManager.GET_ACTIVITIES);
Intent intent = pm.getLaunchIntentForPackage(LiveAppPackage);
if (intent == null) {
throw new PackageManager.NameNotFoundException();
}
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(intent);

}
catch (PackageManager.NameNotFoundException e) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
ActivityThis);

DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_POSITIVE){
//Try to open Market and if fails, open play store.
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse("market://details?id="
+ LiveAppPackage)));
} catch (android.content.ActivityNotFoundException e1) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id="
+ LiveAppPackage)));
}

} else{
dialog.dismiss();
}
}
};

alertDialogBuilder.setTitle("App Title")
.setCancelable(true)
.setPositiveButton("Install", onClickListener);
alertDialogBuilder.setNegativeButton("Cancel", onClickListener);

alertDialogBuilder.setMessage("Second app is not currently installed\n\nLike to install it?"); //Change to your message

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
}
}

How to check whether an application is installed or not when installing another dependent application in android

There is no way for your code to run at the time of installation because, by definition, your code is not yet on the device. And there is no way at the present time to specify some sort of dependency on another app that the Play Store (or any other distribution channel AFAIK) will honor.

Once the code is on the device, and the user runs your launcher activity, you can see if the other app is installed and, if not, prompt the user to install it.

Detect an application is installed or not?

From Android How-to's

If you ever need to know if a particular app is installed on the user's device, you can use the PackageManager. From a Context class (e.g. an Activity or a Service) you can call getPackageManager(). This gives you a variety of methods, one of which is getPackageInfo(). Below is a method you might use. You might call it like this:

isAppInstalled("com.simexusa.campusmaps_full");

private boolean isAppInstalled(String packageName) {
PackageManager pm = getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}


Related Topics



Leave a reply



Submit