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 int getVersionCode(Context con) {
    PackageManager pm = con.getPackageManager();
    PackageInfo pi = null;//from  ww w .  j a v  a  2  s .  c  o  m
    try {
        pi = pm.getPackageInfo(con.getPackageName(), 0);
        return pi.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static String getPackageName(Context contex) {
    PackageManager manager = contex.getPackageManager();
    try {//from  w w  w .j  av  a 2 s .co  m
        PackageInfo info = manager.getPackageInfo(contex.getPackageName(), 0);
        return info.packageName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getVersionName(Context con) {
    PackageManager pm = con.getPackageManager();
    PackageInfo pi = null;//from   w  w  w .  java  2 s  . c  o  m
    try {
        pi = pm.getPackageInfo(con.getPackageName(), 0);
        return pi.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

/**
 * Get app version code/*from   ww  w .j a  va2 s .  c  o m*/
 *
 * @param context
 * @param packageName
 * @return
 */
public static int getAppVersionCode(Context context, String packageName) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:Main.java

public static int GetVersionCode(final Context con) {
    int version = 1;
    PackageManager packageManager = con.getPackageManager();
    try {//from   w ww  .  j  av  a 2 s.c om
        PackageInfo packageInfo = packageManager.getPackageInfo(con.getPackageName(), 0);
        version = packageInfo.versionCode;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return version;
}

From source file:Main.java

/**
 * Get app permission/* ww w  .  j  a  v  a 2  s  . c  o m*/
 *
 * @param context
 * @param packageName
 * @return
 */
public static String[] getAppPermission(Context context, String packageName) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        return packageInfo.requestedPermissions;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean isAppInstalled(Context context, String pkg) {
    PackageManager pm = context.getPackageManager();
    boolean installed = false;
    try {//w w  w  .j av a 2 s. co m
        pm.getPackageInfo(pkg, PackageManager.GET_ACTIVITIES);
        installed = true;
    } catch (Exception e) {
        installed = false;
    }
    return installed;
}

From source file:Main.java

/**
 * Get app signature//w  ww .j a va  2 s .  com
 *
 * @param context
 * @param packageName
 * @return
 */
public static String getAppSignature(Context context, String packageName) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        return packageInfo.signatures[0].toCharsString();
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static boolean isAppInstalled(String uri, Context context) {
    PackageManager pm = context.getPackageManager();
    boolean installed = false;
    try {/* w ww  . jav a  2  s  . com*/
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        installed = false;
    }
    return installed;
}

From source file:Main.java

/**
 * Get app version name/*from  www.j a va  2 s  . co  m*/
 *
 * @param context
 * @param packageName
 * @return
 */
public static String getAppVersionName(Context context, String packageName) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
        return packageInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}