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

前端 未结 30 2385
刺人心
刺人心 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:33

     package com.sqisland.android.versionview;
    
    import android.app.Activity;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        TextView textViewversionName = (TextView) findViewById(R.id.text);
    
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            textViewversionName.setText(packageInfo.versionName);
    
        }
        catch (PackageManager.NameNotFoundException e) {
    
        }
    
      }
    }
    

提交回复
热议问题