Example usage for android.content.pm PackageManager getPackageInfo

List of usage examples for android.content.pm PackageManager getPackageInfo

Introduction

In this page you can find the example usage for android.content.pm PackageManager getPackageInfo.

Prototype

public abstract PackageInfo getPackageInfo(VersionedPackage versionedPackage, @PackageInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve overall information about an application package that is installed on the system.

Usage

From source file:Main.java

public static boolean isAppInstalled(Context context, String pkgName) {
    PackageManager pm = context.getPackageManager();
    boolean installed = false;
    try {//from   www  .  j a  va 2s. c  o m
        pm.getPackageInfo(pkgName, PackageManager.GET_ACTIVITIES);
        installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        installed = false;
    }
    return installed;
}

From source file:Main.java

public static boolean isAppInstalled(String uri, Context context) {
    PackageManager pm = context.getPackageManager();
    boolean app_installed;

    try {/*  w w  w .  j  a v a 2s.  c o m*/
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }

    return app_installed;
}

From source file:Main.java

public static PackageInfo getPackageInfo(Context context, String packageName) {

    PackageInfo packageInfo = null;//from  w  w  w.  j  a v a2  s  .  co m
    PackageManager pm = context.getPackageManager();

    try {
        packageInfo = pm.getPackageInfo(packageName, 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

    return packageInfo;
}

From source file:Main.java

public static int getCurrentAppVersionCode(Context context) {
    int versionCode = 1;
    PackageManager packageManager = context.getPackageManager();
    try {/*ww w  .j  av a  2s . c o  m*/
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        versionCode = packInfo.versionCode;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return versionCode;
}

From source file:Main.java

/**
 * Retrieves the version of the given package installed on the device.
 *
 * @param context Any context./*from  w  ww . j  a v a2s  . co m*/
 * @param packageName Name of the package to find.
 * @return The package's version code if found, -1 otherwise.
 */
public static int getPackageVersion(Context context, String packageName) {
    int versionCode = -1;
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
        if (packageInfo != null)
            versionCode = packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        // Do nothing, versionCode stays -1
    }
    return versionCode;
}

From source file:Main.java

public static boolean isInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    boolean installed = false;
    try {//  w  w  w  .j a v  a2s. co m
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        installed = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return installed;
}

From source file:Main.java

public static String getCurrentAppVersion(Context context) {
    String version = "1.0.0";
    PackageManager packageManager = context.getPackageManager();
    try {/*from   w  w  w.ja  va  2  s  .co m*/
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        version = packInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}

From source file:Main.java

public static int getVersionCode(Context context) {
    try {//w w  w  .  ja  v a2 s  . c o  m
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        int versionCode = info.versionCode;
        return versionCode;
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static boolean isAppInstalled(String uri, Context context) {
    PackageManager pm = context.getPackageManager();
    boolean app_installed;

    try {/*from  w  w  w  .j  a  v a2  s.c om*/
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (NameNotFoundException e) {
        app_installed = false;
    }

    return app_installed;
}

From source file:Main.java

/**
 * Return whether a package is installed.
 *//*from  w  ww .j a  v a2  s .  c  om*/
public static boolean isPackageInstalledAndEnabled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info;
    try {
        info = pm.getPackageInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        info = null;
    }

    return info != null && info.applicationInfo != null && info.applicationInfo.enabled;
}