赞
踩
标签meta-data的官方介绍链接:查看Android官方解释
meta-data是Android的Manifest文件内activity,activity-alias,application,provider,receiver,service组件下的子标签。用于标识供父组件使用的键值对(name-value pair),如果想在activity中使用meta-data,那么meta-data必须定义在AndroidManifest.xml的activity组件中声明。
1:首先,需要在AndroidManifest.xml文件中声明,如下:meta-data的使用就跟一般的键值对使用一样,在对应的组件下声明name和value如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.angcyo.metadatademo">
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <meta-data android:name="msg" android:resource="@string/app_name" />//注意此处
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name"
- android:theme="@style/AppTheme.NoActionBar">
- <meta-data android:name="msg" android:value="angcyo test" />//注意此处
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <service android:name=".TestService">
- <meta-data android:name="msg" android:value="angcyo service data" /> //注意此处
- </service>
-
- <receiver android:name=".TestReceiver">
- <meta-data android:name="msg" android:resource="@string/action_settings" />//注意此处
- </receiver>
- </application>
-
- </manifest>
2:在代码中获取对应的值
- private void init() {
- TextView textView = (TextView) findViewById(R.id.text);
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("app data:");
- stringBuilder.append(getAppMeta());
- stringBuilder.append("\n");
-
- stringBuilder.append("act data:");
- stringBuilder.append(getActMeta());
- stringBuilder.append("\n");
-
- stringBuilder.append("ser data:");
- stringBuilder.append(getSerMeta());
- stringBuilder.append("\n");
-
- stringBuilder.append("rec data:");
- stringBuilder.append(getRecMeta());
- stringBuilder.append("\n");
-
- textView.setText(stringBuilder.toString());
- }
-
- private String getAppMeta() {//app 组件中的meta-data
- try {
- ApplicationInfo info = this.getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
- return getString(info.metaData.getInt(KEY_DATA));//如果是通过 resource 设置的值,需要通过id访问数据
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return "";
- }
-
-
- private String getSerMeta() {//service 组件中的meta-data
- try {
- ServiceInfo info = this.getPackageManager().getServiceInfo(new ComponentName(this, TestService.class), PackageManager.GET_META_DATA);
- return info.metaData.getString(KEY_DATA);
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return "";
- }
-
-
- private String getRecMeta() {//receiver 组件中的meta-data
- try {
- ActivityInfo info = this.getPackageManager().getReceiverInfo(new ComponentName(this, TestReceiver.class), PackageManager.GET_META_DATA);
- return getString(info.metaData.getInt(KEY_DATA));
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return "";
- }
-
-
- private String getActMeta() {//activity 组件中的meta-data
- try {
- ActivityInfo info = this.getPackageManager().getActivityInfo(new ComponentName(this, MainActivity.class), PackageManager.GET_META_DATA);
- return info.metaData.getString(KEY_DATA);
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return "";
- }
使用第三方服务提供的开放API,例如:第三方微信、微博登陆,第三方社交通讯融云SDK、百度/讯飞语音识别SDK等,需要注册对应的账户获取加入第三方服务提供的AppKey、AppSecret;然后在使用SDK时加入这些appKey/appSecret,这时一般采用的都是在应用的Manifest文件application组件下加入指定key供SDK使用者填入申请获得的对应值。
- <!-- 友盟配置appkey和id -->
- <meta-data
- android:name="UMENG_CHANNEL"
- android:value="2">
- </meta-data>
- <meta-data
- android:name="UMENG_APPKEY"
- android:value="************************">
- </meta-data>
- appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
- UMENG_CHANNEL = appInfo.metaData.getString("UMENG_CHANNEL");
- appInfo = this.getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
- UMENG_CHANNEL=appInfo.metaData.getInt("UMENG_CHANNEL");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。