Java Code Examples for android.content.pm.PackageManager.getPackageInfo()

The following are Jave code examples for showing how to use getPackageInfo() of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: android-project-gallery   File: ManifestTools.java   View Source Code Vote up 6 votes
/**
 * 返回版本信息
 * 
 * @param context
 * @return
 */
public static String getAppVersion(Context context)
{
	PackageManager localPackageManager = context.getPackageManager();
	try
	{
		String str = localPackageManager.getPackageInfo(context.getPackageName(), 0).versionName;
		return str;
	}
	catch (PackageManager.NameNotFoundException e)
	{
		Log.E(TAG, "getAppVersion error" + e.getMessage());
		e.printStackTrace();
	}

	return "";
}
 
Example 2
Project: android-AutofillFramework   File: SecurityHelper.java   View Source Code Vote up 6 votes
/**
 * Gets the fingerprint of the signed certificate of a package.
 */
public static String getFingerprint(Context context, String packageName) throws Exception {
    PackageManager pm = context.getPackageManager();
    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
    Signature[] signatures = packageInfo.signatures;
    if (signatures.length != 1) {
        throw new SecurityException(packageName + " has " + signatures.length + " signatures");
    }
    byte[] cert = signatures[0].toByteArray();
    try (InputStream input = new ByteArrayInputStream(cert)) {
        CertificateFactory factory = CertificateFactory.getInstance("X509");
        X509Certificate x509 = (X509Certificate) factory.generateCertificate(input);
        MessageDigest md = MessageDigest.getInstance("SHA256");
        byte[] publicKey = md.digest(x509.getEncoded());
        return toHexFormat(publicKey);
    }
}
 
Example 3
Project: TPlayer   File: VActivityManagerService.java   View Source Code Vote up 6 votes
public void onCreate(Context context) {
    AttributeCache.init(context);
    PackageManager pm = context.getPackageManager();
    PackageInfo packageInfo = null;
    try {
        packageInfo = pm.getPackageInfo(context.getPackageName(),
                PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS | PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    if (packageInfo == null) {
        throw new RuntimeException("Unable to found PackageInfo : " + context.getPackageName());
    }
    sService.set(this);

}
 
Example 4
Project: Android-Code-Demos   File: CrashHandler.java   View Source Code Vote up 6 votes
@SuppressWarnings("deprecation")
private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
    PackageManager pm = mContext.getPackageManager();
    PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
            PackageManager.GET_ACTIVITIES);
    pw.print("App Version: ");
    pw.print(pi.versionName);
    pw.print('_');
    pw.println(pi.versionCode);

    /* Android 版本号 */
    pw.print("OS Version: ");
    pw.print(Build.VERSION.RELEASE);
    pw.print('_');
    pw.println(SDK_INT);

    /* 手机制造商 */
    pw.print("Vendor: ");
    pw.println(Build.MODEL);

    /* CPU 架构 */
    pw.print("CPU ABI: ");
    pw.println(Build.CPU_ABI);
}
 
Example 5
Project: RLibrary   File: RUtils.java   View Source Code Vote up 6 votes
/**
     * 返回app的版本名称.
     *
     * @param context the context
     * @return app version name
     */
    public static String getAppVersionName(Context context) {
        String version = "unknown";
// 获取package manager的实例
        PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
        PackageInfo packInfo;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(),
                    0);
            version = packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
// Log.i("版本名称:", version);
        return version;
    }
 
Example 6
Project: QMUI_Android   File: QMUIPackageHelper.java   View Source Code Vote up 6 votes
/**
 * manifest 中的 versionName 字段
 */
public static String getAppVersion(Context context) {
    if (appVersionName == null) {
        PackageManager manager = context.getPackageManager();
        try {
            android.content.pm.PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            appVersionName = info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (appVersionName == null) {
        return "";
    } else {
        return appVersionName;
    }
}
 
Example 7
Project: cardinalsSample   File: PackageUtils.java   View Source Code Vote up 5 votes
/**
 * 获取应用的versionCode
 *
 * @return int类型的versionCode
 */
public static int getVersionCode(Context context) {

    try {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        return info.versionCode;
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}
 
Example 8
Project: AndroidCollection   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * 获取当前App信息
 * <p>AppInfo(名称,图标,包名,版本号,版本Code,是否安装在SD卡,是否是用户程序)
 */
public static AppInfo getAppInfo(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi = null;
    try {
        pi = pm.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return pi != null ? getBean(pm, pi) : null;
}
 
Example 9
Project: GitHub   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * 获取App路径
 *
 * @param packageName 包名
 * @return App路径
 */
public static String getAppPath(final String packageName) {
    if (isSpace(packageName)) return null;
    try {
        PackageManager pm = Utils.getApp().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.applicationInfo.sourceDir;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 10
Project: GitHub   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * 获取版本号
 */
public static int getAppVersionCode(Context context) {
    int versioncode = -1;
    try {
        // ---get the package info---
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        versioncode = pi.versionCode;
    } catch (Exception e) {
        Log.e("VersionInfo", "Exception", e);
    }
    return versioncode;
}
 
Example 11
Project: MiPushFramework   File: CheckSupportUtils.java   View Source Code Vote up 5 votes
/**
 * Check package is support MiPush.
 * @param pkg Package Name
 * @param manager Package manager
 * @return This package support status. If not use MiPush, will returns null
 */
@Nullable
public static SupportStatus check (String pkg,
                                   PackageManager manager) {
    try {
        PackageInfo info = manager.getPackageInfo(pkg, PackageManager.GET_SERVICES |
                PackageManager.GET_RECEIVERS | PackageManager.GET_DISABLED_COMPONENTS);
        return check(info, manager);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 12
Project: BaseCore   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * 获取App版本号
 *
 * @param context     上下文
 * @param packageName 包名
 * @return App版本号
 */
public static String getAppVersionName(Context context, String packageName) {
    if (StringCovertUtils.isSpace(packageName)) return null;
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 13
Project: GrowingProject   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * 根据包名获取程序的VersionCode
 *
 * @param pContext
 * @param pPackageName
 * @return
 */
public static final int getVersionCode(Context pContext, String pPackageName) {
    PackageManager packageManager = pContext.getPackageManager();
    PackageInfo packageInfo = null;
    try {
        packageInfo = packageManager.getPackageInfo(pPackageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (packageInfo != null) {
        return packageInfo.versionCode;
    } else {
        return UNKNOWN_VERSION_CODE;
    }
}
 
Example 14
Project: Mvp-Retrofit-Rxjava-Rxbus   File: AppInfoUtils.java   View Source Code Vote up 5 votes
/**
 * 获取应用启动图标
 * @param context
 * @return
 * @throws Exception
 */
public static Drawable getApplicationIconDrawable(Context context) throws Exception {  
    PackageManager pm = context.getPackageManager();
    String packageName = getTopActivityPackageName(context);
    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
    Drawable drawable = packageInfo.applicationInfo.loadIcon(pm);
    return drawable;

}
 
Example 15
Project: Utils   File: AppUtil.java   View Source Code Vote up 5 votes
/**
 * 获取App名称
 *
 * @param context     上下文
 * @param packageName 包名
 * @return App名称
 */
public static String getAppName(Context context, String packageName) {
    if (DataUtil.isNullString(packageName))
        return null;
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 16
Project: sflauncher   File: MainActivity.java   View Source Code Vote up 5 votes
public static boolean appInstalled(Context c,String uri){
    PackageManager pm = c.getPackageManager();
    boolean app_installed = false;
    try{
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    }
    catch (PackageManager.NameNotFoundException e){
        app_installed = false;
    }
    return app_installed ;
}
 
Example 17
Project: Utils   File: AppUtil.java   View Source Code Vote up 5 votes
/**
 * 获取App版本号
 *
 * @param context     上下文
 * @param packageName 包名
 * @return App版本号
 */
public static String getAppVersionName(Context context, String packageName) {
    if (DataUtil.isNullString(packageName))
        return null;
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 18
Project: AboutView   File: Utils.java   View Source Code Vote up 5 votes
public static PackageInfo getCurrentPackageInfo(final Context context) {
    final PackageManager pm = context.getPackageManager();
    try {
        return pm.getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
}
 
Example 19
Project: StartThirdApp   File: ActivityStartUtil.java   View Source Code Vote up 5 votes
/**
 * 检查包是否存在
 *
 * @param packageName packageName
 * @return is exit
 */
private static boolean checkPackInfo(PackageManager packageManager, String packageName) {
    PackageInfo packageInfo = null;
    try {
        packageInfo = packageManager.getPackageInfo(packageName, 0);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return packageInfo != null;
}
 
Example 20
Project: AndroidBasicLibs   File: PackageUtil.java   View Source Code Vote up 5 votes
/**
 * get app package info
 */
public static PackageInfo getAppPackageInfo(Context context) {
    if (context != null) {
        PackageManager pm = context.getPackageManager();
        if (pm != null) {
            PackageInfo pi;
            try {
                return pm.getPackageInfo(context.getPackageName(), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}