default

Android 获取应用的版本和版本号

2022-06-22  本文已影响0人  懵懵懂懂_YOYO
1.获取自己应用内部的版本号
    /**
     * 获取自己应用内部的版本号
     */
    public static int getVersionCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }
2.获取自己应用内部的版本名
    /**
     * 获取自己应用内部的版本名
     */
    public static String getVersionName(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }
3.获取其他应用的版本号
public static int getVersionCode(Context context,String packageName) {
    PackageManager manager = context.getPackageManager();
    int code = 0;
    try {
        PackageInfo info = manager.getPackageInfo(packageName, 0);
        code = info.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return code;
}

4.获取其他应用的版本名

public static String getVersionName(Context context,String packageName) {
    PackageManager manager = context.getPackageManager();
    String name = null;
    try {
        PackageInfo info = manager.getPackageInfo(packageName, 0);
        name = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return name;
}
上一篇下一篇

猜你喜欢

热点阅读