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 boolean isIntentAvailable(Context context, Intent i) {
    final PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

From source file:Main.java

public static int getAppVersion(Context context) {
    try {/*www  . j av  a  2s  .c  om*/
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        throw new RuntimeException("Coult not get package name: " + e);
    }
}

From source file:Main.java

public static final String getApplicationVersionName(Context context) {
    try {//from   w  ww.j av a 2  s .co m
        final PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return pInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Check if application with specific package name is installed on device*/
public static boolean isAppInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {//from  www  . j a  va 2s .  c  o m
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

/**
 * Checks whether any version of messenger is installed.
 *
 * @param context an android context//  ww  w .j av a  2 s  .  c o m
 * @return whether any version of messenger is installed
 */
public static boolean hasMessengerInstalled(Context context) {
    try {
        context.getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static int getAppVersionCode(Context c) {
    try {//from  ww  w.  j a  v  a 2  s.  c  o  m
        PackageManager manager = c.getPackageManager();
        PackageInfo info = manager.getPackageInfo(c.getPackageName(), 0);
        return info.versionCode;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static PackageInfo getPackageInfo(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {/*from   www .ja  v a2 s.c  o m*/
        return pm.getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getApkPackageName(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;// w  ww  .  j  a v a  2  s.  c  o  m
        appInfo.publicSourceDir = apkPath;
        return appInfo.packageName;
    }
    return "";
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    try {//from  w  w  w .j  av a2s  .  c o  m
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getVersionName(Context context) {
    try {/*from   www .  j av a2s .c o  m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        if (info != null) {
            return info.versionName;
        }
    } catch (Exception e) {

    }
    return "";
}