Android开发:获取安卓App版本号的方法步骤

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/CC1991_/article/details/101844504

今天国庆节,在这举国欢庆的日子里,发一篇博文留念一下这个特殊的日子,国庆依然奋战在工作一线。  

在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="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://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;

}

具体实现效果如下所示:

 

以上就是本章全部内容,欢迎关注三掌柜的微信公众号“iOS开发by三掌柜”,三掌柜的新浪微博“三掌柜666”,欢迎关注!

三掌柜的微信公众号:

三掌柜的新浪微博:

猜你喜欢

转载自blog.csdn.net/CC1991_/article/details/101844504