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) {
    PackageManager manager = context.getPackageManager();
    try {//ww w.  j  a v a2 s .  c  om
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        return info.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return 0;
}

From source file:Main.java

public static String getVersion(Context context) {
    PackageManager manager = context.getPackageManager();
    try {/* w  ww.java2 s.  c o  m*/
        PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static void runApp(Context context, String appPackageName) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
    context.startActivity(intent);//from w  ww.  j  ava2  s. c o  m
}

From source file:Main.java

public static String getSDKVersion(Context context) {
    PackageManager pm = context.getPackageManager();
    String pkgName = context.getPackageName();
    PackageInfo pkgInfo = null;//w w  w .ja  v a  2s. c om
    String ret = "";
    try {
        pkgInfo = pm.getPackageInfo(pkgName, PackageManager.GET_CONFIGURATIONS);
        ret = pkgInfo.versionName;
    } catch (NameNotFoundException ex) {

    }
    return ret;
}

From source file:Main.java

public static boolean hasCamera(Context context) {
    PackageManager packageManager = context.getPackageManager();
    if (!packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
        return false;
    }//from w w  w.  j  a  va 2 s. com

    return true;
}

From source file:Main.java

/**
 * Function to check if the device has a camera or not.
 * @param context context of class.//from  ww w .j  ava2 s.  c  om
 * @return true if the camera is present and false otherwise.
 */
public static boolean isCameraPresent(Context context) {
    PackageManager packageManager = context.getPackageManager();
    return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)
            || packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
}

From source file:Main.java

public static boolean isMIUIRom(final Context context) {
    try {//from   w  w  w  .  ja  va  2  s. com
        return context.getPackageManager().getPackageInfo("com.miui.cloudservice",
                PackageManager.GET_CONFIGURATIONS) != null;
    } catch (final NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

private static int getUid(Context context) {
    try {//from  ww w . ja  va 2  s .co m
        int uid = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).uid;
        return uid;
    } catch (Exception e) {

    }
    return 0;
}

From source file:Main.java

public static int getAppIconResId(Context app) {
    PackageManager pm = app.getPackageManager();
    String packageName = app.getPackageName();
    try {//from   ww  w.  j  av a 2  s.  c  om
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        return ai.icon;
    } catch (Exception e) {
        e.printStackTrace();
        try {
            return app.getResources().getIdentifier("sym_def_app_icon", "mipmap", "android");
        } catch (Exception e1) {
            e1.printStackTrace();
            return 0;
        }
    }
}

From source file:Main.java

public static boolean isAppInstalled(String uri, Context context) {
    PackageManager pm = context.getPackageManager();
    boolean app_installed;

    try {//from  www.j  av  a2s  .  c om
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (NameNotFoundException e) {
        app_installed = false;
    }

    return app_installed;
}