• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

私藏项目实操分享Android开发获取安卓App版本号的方法步骤

android 搞代码 3年前 (2022-03-01) 27次浏览 已收录 0个评论

前言

在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收费支付


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:私藏项目实操分享Android开发获取安卓App版本号的方法步骤

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址