功能:获取版本号

10年积累的成都做网站、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有那曲免费网站建设让你可以放心的选择与我们合作。
思路:版本号位置在AndroidManifest.xml文件中,需要找到一个类可以获取该位置的信息
需要使用到getPackageInfo和 PackageManager 这两个类。
一、getPackageInfo介绍和用法
介绍:PackageInfo类封装了从配置文件(AndroidManifest.xml)中获取的所有信息,描述了包内容的整体信息。
Overall information about the contents of a package. This corresponds to all of the information collected from AndroidManifest.xml.
二、PackageManager介绍和用法
介绍:PackageManager是一个检索当前已安装在设备上的相关应用程序包的各种信息的类。
Class for retrieving various kinds of information related to the application
packages that are currently installed on the device. You can find this class
through Context.getPackageManager
使用方法:
1.PackageManager对象的获取,Context对象提供了getPackageManager()方法。
2.getPackageInfo方法获取PackageInfo对象,该方法需要传递两个参数:应用包名packageName 和条件flags。
packageName :The full name (i.e. com.google.apps.contacts) of the desired package.
flags: Additional option flags. Use any combination of
GET_ACTIVITIES,GET_GIDS,GET_CONFIGURATIONS,GET_INSTRUMENTATION,GET_PERMISSIONS,GET_PROVIDERS,GET_RECEIVERS,GET_SERVICES,GET_SIGNATURES,GET_UNINSTALLED_PACKAGESto modify the data returned.
代码书写流程:
使用Context.getPackageManager获取PackageManager对象
使用PackageManager对象的getPackageInfo方法获取PackageInfo对象
获取PackageInfo的属性versionCode 或者其他信息
/**
* 获取版本号
*
* @return 当前应用的版本名
*/
public int getVersion() {
try {
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
String version = info.versionName;//版本的名称 用于显示在welcome界面下角
int versionCode = info.versionCode;//版本的code 用于比较升级
return versionCode;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}参考文章:
http://www.cnblogs.com/yeahui/archive/2012/10/20/2732429.html
标题名称:Android获取应用程序版本号
本文链接:http://www.jxjierui.cn/article/jieiso.html


咨询
建站咨询
