Example usage for android.app Activity getPackageManager

List of usage examples for android.app Activity getPackageManager

Introduction

In this page you can find the example usage for android.app Activity getPackageManager.

Prototype

@Override
    public PackageManager getPackageManager() 

Source Link

Usage

From source file:Main.java

/**
 * Get application version.//ww w . ja  v  a  2 s  .  c  om
 *
 * @param activity
 *          current activity.
 * @return application version. This equals to the version that appears in
 *         settings->application.
 * @throws android.content.pm.PackageManager.NameNotFoundException
 */
public static String getAppVersion(Activity activity) throws PackageManager.NameNotFoundException {
    PackageManager pm = activity.getPackageManager();
    PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), 0);
    return pi.versionName;
}

From source file:Main.java

/**
 * Get application name.// w  w w  . j av  a2  s. c  om
 *
 * @param activity
 *          current activity.
 * @return application name. This equals to the app title that appears in
 *         settings->application.
 * @throws android.content.pm.PackageManager.NameNotFoundException
 */
public static String getAppName(Activity activity) throws PackageManager.NameNotFoundException {
    PackageManager pm = activity.getPackageManager();
    ApplicationInfo appInfo = pm.getApplicationInfo(activity.getPackageName(), 0);
    return pm.getApplicationLabel(appInfo).toString();
}

From source file:Main.java

public static boolean isZhihuInstalled(Activity activity) {
    PackageInfo packageInfo;//from  w w  w. ja va 2 s  .  co m
    try {
        packageInfo = activity.getPackageManager().getPackageInfo("com.zhihu.android", 0);
    } catch (PackageManager.NameNotFoundException e) {
        packageInfo = null;
        e.printStackTrace();
    }

    return (packageInfo == null) ? false : true;
}

From source file:Main.java

public static int getVersionNumber(Activity act) {

    int versionCode = -1;
    try {/*from  w  w  w.  j  a  va2  s . c o  m*/
        PackageInfo packageInfo = act.getPackageManager().getPackageInfo(act.getPackageName(), 0);
        versionCode = packageInfo.versionCode;
    } catch (Exception ex) {
        System.out.println("Version Num : " + ex);
    }
    return versionCode;
}

From source file:Main.java

public static String getBuildTimestamp(Activity activity) {
    String s = "";
    try {//from  w  ww. j a  v  a 2 s . co m
        ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0);
        ZipFile zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        s = SimpleDateFormat.getInstance().format(new java.util.Date(time));
        zf.close();
    } catch (Exception e) {
    }
    return s;
}

From source file:Main.java

/**
 * Get activity meta-data.//from  www .j av  a2s . com
 * @param activity activity to get meta-data from.
 * @return meta-data, may be empty but never null.
 */
public static Bundle getActivityMetaData(Activity activity) {
    Bundle config;
    try {
        config = activity.getPackageManager().getActivityInfo(activity.getComponentName(),
                GET_META_DATA).metaData;
        if (config == null)
            config = new Bundle();
    } catch (Exception e) {
        /*
         * NameNotFoundException or in some rare scenario an undocumented "RuntimeException: Package
         * manager has died.", probably caused by a system app process crash.
         */
        config = new Bundle();
    }
    return config;
}

From source file:Main.java

public static Intent getLiveChannels(Activity mActivity) {
    if (isPackageInstalled(ANDROID_TV_LIVE_CHANNELS, mActivity)) {
        Intent i = mActivity.getPackageManager().getLaunchIntentForPackage(ANDROID_TV_LIVE_CHANNELS);
        return i;
    } else if (isPackageInstalled(SONY_LIVE_CHANNELS, mActivity)) {
        Intent i = mActivity.getPackageManager().getLaunchIntentForPackage(SONY_LIVE_CHANNELS);
        return i;
    }//from www  . j  av  a2 s.co  m
    return null;
}

From source file:Main.java

public static void printKeyHash(Activity pActivity) {
    // Add code to print out the key hash
    try {//from   w  w  w.  j av a2  s.  c om
        PackageInfo info = pActivity.getPackageManager().getPackageInfo(pActivity.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("KeyHash:", e.toString());
    } catch (NoSuchAlgorithmException e) {
        Log.d("KeyHash:", e.toString());
    }
}

From source file:Main.java

public static String getActivityMetaData(Activity activity, String key) {
    String data = null;/*from www  .  jav  a  2s. c om*/
    ActivityInfo info = null;
    try {
        info = activity.getPackageManager().getActivityInfo(activity.getComponentName(),
                PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    if (info != null) {
        data = info.metaData.get(key).toString();
    }
    return data;
}

From source file:com.android.contacts.activities.RequestPermissionsActivity.java

public static boolean startPermissionActivityIfNeeded(Activity activity) {
    return startPermissionActivity(activity, getPermissions(activity.getPackageManager()),
            RequestPermissionsActivity.class);
}