前言

在Android开发过程中,想要开发一个残缺性能的App,各个中央的内容都要波及到,比方获取App的零碎版本号就是必须要有的性能。Android的App版本号相干内容比iOS的App版本号内容要多,而且iOS版的App版本信息跟Android的还不一样。本篇文章就来介绍一下Android开发中获取App版本号的办法步骤,不便当前应用。获取App版本号罕用的有两个办法,这两种形式都能获取到零碎版本号,请依据理论需要或者偏好来抉择任何一种办法即可。
办法一:

1、关上我的项目工程,找到左侧我的项目目录外面的app目录下的build.gradle文件,而后单击进入,而后找到defaultConfig文件外面的“versionName”选项,这个选项对应的就是零碎版本号信息。

2、在须要展现App零碎版本号的xml文件外面进行布局,具体代码如下所示:

<?xml version="1.0" encoding="utf-8"?>  <layout>  <RelativeLayout xmlns:android="schemas.android.com/apk/res/and…"      xmlns:app="schemas.android.com/apk/res-aut…"      xmlns:tools="schemas.android.com/tools"      android:layout\_width="match\_parent"      android:layout\_height="match\_parent"      android:orientation="vertical"      android:background="#2B2C2D"      tools:context="com.mvvm.activity.TeaMineVersionActivity">      <RelativeLayout          app:layout\_scrollFlags="scroll|enterAlways"          android:id="@+id/toobar"          android:layout\_width="match\_parent"          android:layout\_height="match\_parent">          <ImageView              android:id="@+id/version\_i"              android:layout\_marginTop="@dimen/dp50"              android:layout\_width="140dp"              android:layout\_height="140dp"              android:layout\_centerHorizontal="true"              android:background="@color/white"              android:src="@drawable/mine\_version"/>          <TextView              android:id="@+id/showVersion"              android:layout\_width="match\_parent"              android:layout\_height="40dp"              android:layout\_centerHorizontal="true"              android:layout\_below="@+id/version\_i"              android:textSize="@dimen/dp16"              android:textAlignment="center"              android:textColor="@color/white"              android:layout\_marginLeft="@dimen/dp20"              android:layout\_marginTop="@dimen/dp20"/>      </RelativeLayout>  </RelativeLayout>  </layout>

3、在java文件外面进行对应获取App版本号的代码操作,具体代码如下所示:

private TextView showVersion = null;  showVersion = (TextView)findViewById(R.id.showVersion);  showVersion.setText("TE: "+getAppVersionName(getApplicationContext()));  public static String getAppVersionName(Context context) {      String versionName = "";      try {          PackageManager pm = context.getPackageManager();          PackageInfo pi = pm.getPackageInfo(context.getPackageName(),0);          versionName = pi.versionName;          if (versionName == null || versionName.length() <= 0) {              return "";          }      } catch (Exception e) {          Log.e("VersionInfo", "Exception", e);      }      return versionName;  }
办法二:

1、具体操作步骤同办法一的步骤1;

2、具体操作步骤同办法一的步骤2;

3、在java文件外面的具体操作代码如下所示:

private TextView showVersion = null;  showVersion = (TextView)findViewById(R.id.showVersion);  showVersion.setText("TE: "+getVersionName());  private String getVersionName() {      String version = "";      try {          //获取PackageManager实例          PackageManager packageManager = getPackageManager();          //getPackageName()是以后类的包名,0示意获取版本信息          PackageInfo packeInfo = packageManager.getPackageInfo(getPackageName(),0);          version = packeInfo.versionName;      } catch (Exception e) {          Log.e("VersionInfo","Exception",e);      }      return version;  }

我的库存,须要的小伙伴请点击我的GitHub收费支付