android获取versionName和versionCode
首先设置一个展示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 block e.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 block e.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,否则会报错
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28