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

/**
 * get app package info/*from   www.  j  a va2 s.  c  o  m*/
 */
public static PackageInfo getAppPackageInfo(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        return pm.getPackageInfo(context.getPackageName(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void clearActivityTask(final Context context) {
    Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(i);/*from w  w w.java2s .co  m*/
    ((Activity) context).finish();

}

From source file:Main.java

public static boolean hasInstalledAlipayClient(Context context) {
    PackageManager pm = context.getPackageManager();
    try {//from   ww w .  j av a2s.  com
        PackageInfo info = pm.getPackageInfo(ALIPAY_PACKAGE_NAME, 0);
        return info != null;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void launchApk(Context context, String packageName) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    context.startActivity(intent);/*w w w . j  a va  2s . com*/
}

From source file:Main.java

public static PackageInfo getCurrentPackageInfo(Context context) {
    try {//from   w w w  . ja  v  a  2 s  .  c  o m
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getVersion(Context context) {
    PackageManager pm = context.getPackageManager();
    try {//from  w  w w  .  ja va  2  s. co  m
        PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
        return info.versionCode + "";
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static PackageInfo packageInfoFromContext(final Context context) {
    try {//from   w  w  w.j a va 2s  . co  m
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (final PackageManager.NameNotFoundException x) {
        throw new RuntimeException(x);
    }
}

From source file:Main.java

public static int getRevision(Context context) {
    try {//from ww w.j a va  2 s .  c o  m
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        return -1;
    }
}

From source file:Main.java

private static PackageInfo getPackageInfo(Context context) {
    try {//from  w  w w .j  av  a2s . c  om
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("Could not get package name: " + e);
    }
}

From source file:Main.java

public static String getAppNameFromPath(Context context, String path) {
    PackageInfo pi = context.getPackageManager().getPackageArchiveInfo(path, 0);

    pi.applicationInfo.sourceDir = path;
    pi.applicationInfo.publicSourceDir = path;
    return pi.applicationInfo.loadLabel(context.getPackageManager()).toString();
}