How to get the build/version number of your Android application?

前端 未结 30 1772
刺人心
刺人心 2020-11-22 11:00

I need to figure out how to get or make a build number for my Android application. I need the build number to display in the UI.

Do I have to do something with

30条回答
  •  不思量自难忘°
    2020-11-22 11:27

    I have SOLVE this by using Preference class.

    package com.example.android;
    
    import android.content.Context;
    import android.preference.Preference;
    import android.util.AttributeSet;
    
    public class VersionPreference extends Preference {
        public VersionPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            String versionName;
            final PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                try {
                    PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
                    versionName = packageInfo.versionName;
                } catch (PackageManager.NameNotFoundException e) {
                    versionName = null;
                }
                setSummary(versionName);
            }
        }
    }
    

提交回复
热议问题