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 String readKey(Context context, String keyName) {
    try {//from w w  w.  j  a v  a2 s  .  c o  m
        return context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).metaData.getString(keyName);
    } catch (PackageManager.NameNotFoundException e) {
        return "Android";
    }
}

From source file:Main.java

public static int getVersionCode(Context context) {

    try {//from  w  w  w .j  a v  a  2 s . c  om
        PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return pi.versionCode;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    try {//from  w  w w. j  a v a2s  .com
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getAppVersionName(Context context, String s) {
    PackageManager manager = context.getPackageManager();

    try {//from   w w w .j  a  v a 2  s  . c o m
        PackageInfo packageInfo = manager.getPackageInfo(s, 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
}

From source file:Main.java

public static String GetVersion(Context context) {
    try {//  ww w.j  a  v a2  s  . c o m
        PackageInfo manager = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return manager.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        return "Unknown";
    }
}

From source file:Main.java

public static int getAppVersionCode(Context context) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo info;//from   w  w w.j  av  a 2 s  .com
    try {
        info = packageManager.getPackageInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return 1;
    }
    return info.versionCode;
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    try {//from  w  ww  .  j  av a  2s.  c  om
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return pInfo.versionName;
    } catch (Exception e) {
        // ignore missing data
    }
    return "unknown";
}

From source file:Main.java

public static int getVersionCode(Context context) {

    PackageManager pm = context.getPackageManager();
    int versionCode = 1;
    try {//from   w ww  . j a va2s .  c o  m
        PackageInfo info = pm.getPackageInfo(context.getPackageName(),
                PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        if (null != info) {
            versionCode = info.versionCode;
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return versionCode;

}

From source file:Main.java

/**
 * Returns true if the intent action is available on the device, false
 * otherwise.//from   w w  w.j a  va2s.  co  m
 */
public static boolean isIntentAvailable(Context ctx, String action) {
    PackageManager pm = ctx.getPackageManager();
    Intent intent = new Intent(action);
    return pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}

From source file:Main.java

public static String getAppName(Context context) {

    PackageManager packageManager = context.getPackageManager();
    try {//from   ww  w.ja  va 2s .co m
        PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        int labelRes = packageInfo.applicationInfo.labelRes;
        return context.getResources().getString(labelRes);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}