首先设置一个展示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,否则会报错

更多相关文章

  1. Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方
  2. Android对应版本号
  3. Android安装卸载程序具体操作方法解析
  4. android软键盘挤压界面的问题解决方法
  5. Android修改APP版本号
  6. Android中数据存储的5种方法
  7. 百度地图android客户端的AndroidMainfest.xml的学习和android版
  8. android中activity全屏的方法

随机推荐

  1. Android(安卓)简单通用的基类
  2. Android(安卓)Studio 中的maven仓库使用
  3. Android三种网络通讯机制介绍及区别
  4. Android高仿360安全卫士--布局篇
  5. Android(安卓)OKHTTP3.4版本封装
  6. 判断手机是否支持google play服务
  7. Android画画板的制作方法
  8. Android数据存储之SQLite数据库存储
  9. Android系统服务(SystemService)简介
  10. Android(安卓)Drawable Resource学习(六)、