Android-->AndroidManifest.xml 文件 使用方法

官方介绍: http://android.xsoftlab.net/guide/topics/manifest/meta-data-element.html

使用方法:
1:首先,需要在AndroidManifest.xml文件中声明,如下:

<?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 "";
}

项目地址:https://github.com/angcyo/MetaDataDemo

至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.

你可能感兴趣的:(android,meta-data)