android获取versionName和versionCode
16lz
2021-01-23
首先设置一个展示Textview
<TextView android:textSize="14.0sp" android:textColor="@color/lightblack" android:id="@+id/tv_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20.0dip" android:text="展示" />
获取versionName方法:
public static String getVersion(Context context)//获取版本号{try {PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return pi.versionName;} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return context.getString(R.string.version_unknown);}}
获取versionCode方法:
public static int getVersionCode(Context context)//获取版本号(内部识别号){try {PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return pi.versionCode;} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return 0;}}
2个获取方法都写在了CustomUtils.java文件中方便调用
具体的调用方法如下:
Textview tv_version;tv_version=(TextView) this.findViewById(R.id.tv_version);tv_version.setText(CustomUtils.getVersion(this));//tv_version.setText(CustomUtils.getVersionCode(this)+"");//之所以加“”是因为获取的versionCode是int类型的数据,加""直接转化为String,否则会报错
更多相关文章
- Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方
- Android对应版本号
- Android安装卸载程序具体操作方法解析
- android软键盘挤压界面的问题解决方法
- Android修改APP版本号
- Android中数据存储的5种方法
- 百度地图android客户端的AndroidMainfest.xml的学习和android版
- android中activity全屏的方法