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

/**
 * Convenience method to test whether the Collector application is installed on the device or not
 *
 * @param context an application context
 * @return true if Collector is installed on the device, false otherwise
 *///ww  w .ja  v a  2  s.  c o m
public static boolean isCollectorInstalled(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(COLLECTOR_URI, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException ignore) {
    }

    return false;
}

From source file:Main.java

public static boolean needsMigration(Context context) {
    try {/*from   ww w.  j  a va 2  s.co  m*/
        int currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(),
                0).versionCode;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

        return !preferences.contains("migration_" + currentVersion);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static void launchApp(Context context, String packageName) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(packageName);
    context.startActivity(intent);/* ww  w  .j  a  va  2 s.  c  om*/
}

From source file:Main.java

public static int getAppVersionCode(Context context) {
    try {//ww  w .j  a  va2  s  . co m
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        return info.versionCode;
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static int getVersionCode(Context appContext) {
    try {//from   ww  w .  j  a va2  s .  c  o  m
        PackageInfo packInfo = appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0);
        return packInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return 1;
    }
}

From source file:Main.java

@SuppressWarnings("unused")
private static boolean isIntentAvailable(Context context, Intent intent) {
    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 getAppName(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {/* w w w  .j a  v  a2s .co  m*/
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        appName = (String) packageManager.getApplicationLabel(applicationInfo);
        if (appName == null)
            appName = "unknown";
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return appName;
}

From source file:Main.java

public static Drawable getIconFromApp(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {//from  w  w w .j a  v a2  s.  co m
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi.applicationInfo.loadIcon(pm);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getAppSign(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
    for (PackageInfo pkg : packages) {
        if (packageName.equals(pkg.packageName)) {
            return pkg.signatures[0].toCharsString();
        }//from  w  w w  .  j a  v a2 s .c om
    }

    return null;
}

From source file:Main.java

public static boolean isExsistServices(Context context, Class<?> mClass) throws NameNotFoundException {
    return context.getPackageManager().getServiceInfo(new ComponentName(context, mClass),
            PackageManager.GET_META_DATA) != null;
}