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

前端 未结 30 2361
刺人心
刺人心 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 11:33

    Example for inside Fragment usage.

            import android.content.pm.PackageManager;
            .......
    
            private String VersionName;
            private String VersionCode;
            .......
    
    
            Context context = getActivity().getApplicationContext();
    
            /*Getting Application Version Name and Code*/
            try
            {
    
                 VersionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    
                 /*I find usefull to convert vervion code into String, so it's ready for TextViev/server side checks*/ 
    
                 VersionCode = Integer.toString(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
            } catch (PackageManager.NameNotFoundException e)
            {
                 e.printStackTrace();
            }
    
    // DO SOMETHING USEFULL WITH THAT
    

提交回复
热议问题