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 int getVersionCode(Context context) {
    try {/*from w w w . j a  va  2 s  . co m*/
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        int version = info.versionCode;
        return version;
    } catch (Exception e) {
        return 0;
    }
}

From source file:Main.java

public static String getAppName(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    String appName = null;//from   w  w  w.  j  a  v  a 2s. c  o m
    try {
        ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
        appName = String.valueOf(pm.getApplicationLabel(applicationInfo));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return appName;
}

From source file:Main.java

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

From source file:Main.java

public static String getVersionCode(Context context) {
    try {// ww w.ja v a  2  s .c om
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        String version = String.format("%d", info.versionCode);
        return version;
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static boolean checkPermission(Context context, String paramString) {
    PackageManager localPackageManager = context.getPackageManager();
    return localPackageManager.checkPermission(paramString, context.getPackageName()) == 0;
}

From source file:Main.java

/**
 * Get the application version (Version Code)
 * @param context//from   ww  w .j  av  a  2s .  co  m
 * @return
 */
public static int getApplicationVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        throw new RuntimeException("Could not get package name: " + e);
    }
}

From source file:Main.java

public static String getVersionName(Context context) throws NameNotFoundException {
    PackageManager manager = context.getPackageManager();
    PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
    return packageInfo.versionName;
}

From source file:Main.java

public static String getVersionName(final Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {//from w  w w.ja va 2 s .c  om
        PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(),
                PackageManager.GET_ACTIVITIES);
        return packageInfo.versionName;
    } catch (NameNotFoundException e) {
        return "Unknown";
    }
}

From source file:Main.java

public static boolean isPackageInstaller(Context appContext, String installer) {
    String installerPackageName = appContext.getPackageManager()
            .getInstallerPackageName(appContext.getPackageName());
    return installerPackageName != null && installerPackageName.equals(installer);
}

From source file:Main.java

/**
 * @param context A context of the current application.
 * @return The application name of the current application.
 *///from ww  w.j a  v a2  s . co  m
public static final String getApplicationName(Context context) {
    final PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo;
    try {
        applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    } catch (final PackageManager.NameNotFoundException e) {
        applicationInfo = null;
    }
    return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo)
            : "(unknown)");
}