Example usage for android.content Context getPackageManager

List of usage examples for android.content Context getPackageManager

Introduction

In this page you can find the example usage for android.content Context getPackageManager.

Prototype

public abstract PackageManager getPackageManager();

Source Link

Document

Return PackageManager instance to find global package information.

Usage

From source file:Main.java

public static PackageInfo getPackageInfo(Context context) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo = null;//from  ww w  . jav  a  2s.c  om
    try {
        packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return packageInfo;
}

From source file:Main.java

private static boolean isIntentAvailable(Context context, Intent intent) {
    final PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
    return list.size() > 0;
}

From source file:Main.java

public static String getVersionName(Context context) {
    PackageManager p = context.getPackageManager();
    // GetPackageName () is your current class package name, 0 stands for is
    // to get version information
    PackageInfo packInfo;//  ww  w . j  a va  2  s  .  c o  m
    try {
        packInfo = p.getPackageInfo(context.getPackageName(), 0);
        return packInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

/**
 * @return Application's version code from the {@code PackageManager}.
 *//* w w  w  .  java2  s.  c  o m*/
private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        // should never happen
        throw new RuntimeException("Could not get package name: " + e);
    }
}

From source file:Main.java

public static int getVersionCode(Context context) {
    try {/*from  w w w .  j a va2  s .c  o m*/
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        int version = info.versionCode;
        return version;
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static String getVersion(Context context) {
    try {// w w w.j  a  v a  2 s  .  c om
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        String version = info.versionName;
        return version;
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    try {/*from  w  w  w .  j  a v a  2 s  .  c o  m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);
        return info.versionName;
    } catch (NameNotFoundException e) {
        return "";
    }
}

From source file:Main.java

/**
 * Return whether a package is installed.
 *///from ww  w  .  j  av  a 2  s . c  o m
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;
}

From source file:Main.java

public static boolean isUpToDate(String v2, Context context) {
    try {//w  ww  . ja v  a 2  s. c  o  m
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        String version = info.versionName;

        return v2.equals(version);

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

/**
 * Get meta data of named node: fix bug for integer values
 *///from   w  w  w.  j a  va  2s  . c o m
public static String getAppMetaData(Context context, String name) {
    try {
        return context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).metaData.get(name).toString();
    } catch (Exception e) {
        return null;
    }
}