Android之读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)
AndroidManifest.xml中的定义如下: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="hpc.cai.test01" android:versionCode="1" android:versionName="1.0" > <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <meta-data android:name="tel" android:value="cdma-13366350377" > </meta-data> <meta-data android:name="channel" android:value="eben" /> <activity android:name="hpc.cai.test.Test1Activity" android:label="@string/app_name" > <meta-data android:name="name" android:value="自强不息" > </meta-data> <meta-data android:name="city" android:value="北京" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
package net.duohuo.lequ.activity.other; import android.app.Activity; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; /** * Created by ChenZhiNing on 2015/7/30. */ public class MetaDataHelper { private Activity mActivity; public MetaDataHelper(Activity mActivity) { this.mActivity = mActivity; } /** * activity MetaData读取 */ private void testActivityMetaData() { ActivityInfo info; try { info = mActivity.getPackageManager().getActivityInfo(mActivity.getComponentName(), PackageManager.GET_META_DATA); String msg = info.metaData.getString("name"); System.out.println("name:" + msg); msg = info.metaData.getString("city"); System.out.println("city:" + msg); } catch (Exception e) { e.printStackTrace(); } } /** * appliction MetaData读取 */ private void testApplicationMetaData() { ApplicationInfo info; try { info = mActivity.getPackageManager().getApplicationInfo(mActivity.getPackageName(), PackageManager.GET_META_DATA); String msg = info.metaData.getString("tel"); System.out.println("tel:" + msg); msg = info.metaData.getString("channel"); System.out.println("channel:" + msg); } catch (Exception e) { e.printStackTrace(); } } /** * 测试版本信息 */ private void getVersion() { PackageInfo pkg; try { pkg = mActivity.getPackageManager().getPackageInfo(mActivity.getApplication().getPackageName(), 0); String appName = pkg.applicationInfo.loadLabel(mActivity.getPackageManager()).toString(); String versionName = pkg.versionName; System.out.println("appName:" + appName); System.out.println("versionName:" + versionName); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } }