is Apk File Installed - Android App

Android examples for App:APK Install and Uninstall

Description

is Apk File Installed

Demo Code


//package com.java2s;
import java.util.List;
import android.content.Context;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {

    public static boolean isApkFileInstalled(Context context,
            String archiveFilePath) {
        if (null == archiveFilePath) {
            throw new IllegalArgumentException(
                    "archiveFilePath cannot be null");
        }/*from  w  w  w.  j a v  a2  s  .  com*/
        PackageInfo info = getPackageInfo(context, archiveFilePath);
        if (info == null) {
            //apk????
            return false;
        }
        return isApkInstalled(context, info.packageName);
    }

    public static PackageInfo getPackageInfo(Context context,
            String archiveFilePath) {
        PackageManager pm = context.getPackageManager();
        return pm.getPackageArchiveInfo(archiveFilePath,
                PackageManager.GET_ACTIVITIES);
    }

    public static boolean isApkInstalled(Context context, String packageName) {
        PackageManager pkgManager = context.getPackageManager();

        List<PackageInfo> pkgs = pkgManager
                .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
        for (PackageInfo pkg : pkgs) {
            if (packageName.equals(pkg.packageName)) {
                return true;
            }
        }
        return false;
    }

    public static List<PackageInfo> getInstalledPackages(Context context) {
        PackageManager pkgManager = context.getPackageManager();
        return pkgManager
                .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
    }
}

Related Tutorials