Android meta-data值的获取

meta-data 可能存在

1).Activity 中

<activity android:name=".MainActivity">
    <meta-data android:name="test" android:value="12313" />
</activity>

获取方法

try {
    ActivityInfo info = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
    String value = info.metaData.getString("data_activity");
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

2).Application中

<meta-data android:name="meta_application" android:value="data_application" />

获取方法

try {
    ApplicationInfo appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
    String value=appInfo.metaData.getString("data_application");
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

3).Service中

<service android:name=".MyService" >
    <meta-data android:name="service_meta" android:value="data_service" />
</service>

获取方法

try {
    ComponentName cn=new ComponentName(this, MyService.class);
    ServiceInfo info=getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
    String value=info.metaData.getString("data_service");
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

4).Receiver中

<receiver android:name=".MyReceiver">
    <meta-data android:name="data_receiver" android:value="data_receiver" />
</receiver>

获取方法

try {
    ComponentName cn=new ComponentName(this, MyReceiver.class);
    ActivityInfo info=getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
    String value=info.metaData.getString("data_receiver");
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}