您的位置:首页 > 移动开发 > Android开发

在Fragment中获取AndroidManifest.xml根元素(<manifest>)下的版本信息

2016-09-08 15:56 435 查看
String appVersionName=null;

try{

PackageManage manage=this.getActivity().getPackageManage();
PackageInfo   info=manage.getPackageInfo(
this.getActicity().getPackageName(),0);

appVersionName=info.versionName;
}catch(NameNotFoundException e){
e.printStackTrace();
}


得到应用程序信息的方法getPackageInfo(PackageName,flags)的源码解释如下:

/**
* Retrieve overall information about an application package that is
* installed on the system.
* <p>
* Throws {@link NameNotFoundException} if a package with the given name can
* not be found on the system.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of the
*            desired package.
* @param flags Additional option flags. Use any combination of
*  {@link #GET_ACTIVITIES}, {@link #GET_GIDS},
*  {@link #GET_CONFIGURATIONS}, {@link #GET_INSTRUMENTATION},
*  {@link #GET_PERMISSIONS}, {@link #GET_PROVIDERS},
*  {@link #GET_RECEIVERS}, {@link #GET_SERVICES},
*  {@link #GET_SIGNATURES}, {@link #GET_UNINSTALLED_PACKAGES} to
*            modify the data returned.
* @return Returns a PackageInfo object containing information about the
*         package. If flag GET_UNINSTALLED_PACKAGES is set and if the
*         package is not found in the list of installed applications, the
*         package information is retrieved from the list of uninstalled
*         applications (which includes installed applications as well as
*         applications with data directory i.e. applications which had been
*         deleted with {@code DONT_DELETE_DATA} flag set).
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
* @see #GET_UNINSTALLED_PACKAGES
*/


大意为会在系统已安装的应用程序中检索这个包的信息,如果这个包的名字在这个系统中没有被找到的话就会抛出一个NameNotFoundException,参数PackageName是期望的包名称,参数Flags有多种选择会根据不同选择返回不同的数据,如果flags设置的是GET_UNINSTALLED_PACKAGES并且这个包是不会被找到的在已安装的应用程序集合中,这个包的信息检索就会是来自于未安装的应用程序集合(这个集合还包含已经安装但是被删掉的应用程序的数据信息)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: