标签:
<meta-data android:name="data_name" android:value="@string/meta_data" android:resource="@string/meta_data"/>类似于一个键值对的形式,使用value则保存的是对应的String等值,使用resource则保存的是对应的资源Id值。
<activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <meta-data android:name="data_name" android:value="@string/meta_data" /> </activity>
// 使用时注意加上try_catch ActivityInfo actInfo = this.getPackageManager().getActivityInfo( getComponentName(), PackageManager.GET_META_DATA); String msg = actInfo.metaData.getString("data_name"); // 如果meta-data类型是资源ID,使用getInt获取资源ID即可
<service android:name=".MyService"> <meta-data android:name="data_name" android:value="@string/meta_data"/> </service>
ComponentName cn = new ComponentName(this, MyService.class); ServiceInfo serInfo = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA); String msg = serInfo.metaData.getString("data_name");
<receiver android:name=".MyReceiver"> <meta-data android:name="data_name" android:value="@string/meta_data"/> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> </intent-filter> </receiver>
ComponentName cn = new ComponentName(this, MyReceiver.class); ActivityInfo info = this.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA); String msg = info.metaData.getString("data_name");
<application> <meta-data android:name="data_name" android:value="@string/meta_data"/> </application>
ApplicationInfo appInfo = this.getPackageManager().getApplicationInfo( getPackageName(), PackageManager.GET_META_DATA); String msg = appInfo.metaData.getString("application_name");
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/woliuyunyicai/article/details/47319417