Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying if an app exists, if not go to play store

Tags:

android

a bit of a doozy.

What I would like to know is that is it possible that if an app doesnt exist on a device, can it go into the play store to download it. I know I need to put this code in

Intent i = getPackageManager().getLaunchIntentForPackage("com.package.address");
    startActivity(i);

But if that doesnt exist, can I then get it to go to the Play Store

like image 520
j1mmyg88 Avatar asked Sep 03 '13 15:09

j1mmyg88


1 Answers

You can use one of the following function to check whether the app is installed or not.

Function 1

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

Or Function 2

public boolean isAppInstalled(String targetPackage){
    List<ApplicationInfo> packages;
    PackageManager pm = getPackageManager();        
    packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if(packageInfo.packageName.equals(targetPackage)) return true;
    }        
    return false;
}

USAGE

if(isAppInstalled("com.package.name")){
    //Your Code
}
else{
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.package.name")));
}
like image 141
Sunil Mishra Avatar answered Dec 25 '22 06:12

Sunil Mishra