赞
踩
AndroidManifest.xml中定义了meta-data,例如常见的渠道名:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="zhangphil.book">
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
-
- ......
-
- <meta-data
- android:name="APP_CHANNEL"
- android:value="zhangphil" />
- </application>
- </manifest>
上层Java代码读取:
- ApplicationInfo appInfo;
- try {
- appInfo = this.getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
- Bundle bundle = appInfo.metaData;
- String app_channel = bundle.getString("APP_CHANNEL", null);
-
- Toast.makeText(getApplicationContext(), app_channel, Toast.LENGTH_SHORT).show();
- } catch (Exception e) {
- e.printStackTrace();
- }
需要注意的是,如果meta-data不是定义在application里面,而是在activity或者service等Android组件里面,代码实现会有不同。如果meta-data定义在service里面,那么上层Java代码读取的方式为:
- ComponentName cn = new ComponentName(this, XXXService.class);
- ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
- String xxx_value = info.metaData.getString("xxx_key");
若是activity,则形式类似,用到的关键组件是activity的。如果是广播broadcast,则是广播的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。