There are two ways to know that the particular application is installed in your android device or not.
1st way:-
In first way we get the list of installed application and check whether the targeted package name (package of the application that we want to check) is present or not. If the target package is present that means the particular application is installed otherwise not.
Code:-
public boolean isApplicationInstalled(String targetPackage) {
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if (packageInfo.packageName.equals(targetPackage)) {
// Application found
return true;
}
}
// Application not found
return false;
}
2nd way:-
In a second way we check that the particular package information is available or not without getting the list of all apps installed on device and itterating through them. If the information is available that means application is installed otherwise NameNotFoundException will raise which means application is not installed.
Code:-
public boolean isApplicationInstalled(String targetPackage) {
PackageManager pm = getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
// Appllication not found
return false;
}
// Application found
return true;
}
0 Comment(s)