Android 用meta-data配置参数

http://blog.csdn.net/r8HZGEmq/article/details/8228423

一,在activity中

xml中配置如下

[html]  view plain copy
  1. <!-- Activity -->  
  2.         <activity android:name=".MyActivity" android:label="@string/app_name">  
  3.             <intent-filter>  
  4.                 <action android:name="android.intent.action.MAIN" />  
  5.                 <category android:name="android.intent.category.LAUNCHER" />  
  6.             </intent-filter>  
  7.             <meta-data android:name="activity_name" android:value="activity_value" />  
  8.         </activity>  
java代码如下

[html]  view plain copy
  1. ActivityInfo actInfo = mContext.getPackageManager().getActivityInfo(  
  2.                     getComponentName(), PackageManager.GET_META_DATA);  
  3.             String msg = actInfo.metaData.getString("activity_name");  


二,在service中

xml中配置如下

[html]  view plain copy
  1. <!-- 服务 -->  
  2.         <service android:name=".MyService">  
  3.             <meta-data android:name="service_name" android:value="service_value" />  
  4.         </service>  
java代码如下

[html]  view plain copy
  1. ComponentName cn = new ComponentName(this, MyService.class);  
  2.         try {  
  3.             ServiceInfo serInfo = this.getPackageManager().getServiceInfo(cn,  
  4.                     PackageManager.GET_META_DATA);  
  5.         } catch (NameNotFoundException e) {  
  6.             e.printStackTrace();  
  7.         }  
  8.         String msg = serInfo.metaData.getString("service_name");  

三,在application中

xml中配置如下

[html]  view plain copy
  1. <!-- app -->  
  2.         <meta-data android:name="application_name" android:value="application_value" />  
java代码如下

[html]  view plain copy
  1. ApplicationInfo appInfo = this.getPackageManager().getApplicationInfo(  
  2.                     getPackageName(), PackageManager.GET_META_DATA);  
  3.             String msg = appInfo.metaData.getString("application_name");  

四,在receiver中,也就是广播接收者(拨打电话的时候会看到toast消息)
xml中配置如下
[html]  view plain copy
  1. <receiver android:name=".MyReceiver">  
  2.             <meta-data android:name="receiver_name" android:value="receiver_value" />  
  3.             <intent-filter>  
  4.                 <action android:name="android.intent.action.PHONE_STATE" />  
  5.             </intent-filter>  
  6.         </receiver>  
java代码如下

[html]  view plain copy
  1. if (TextUtils.equals("android.intent.action.PHONE_STATE", intent  
  2.                 .getAction())) {  
  3.             ComponentName cn = new ComponentName(context, MyReceiver.class);  
  4.             try {  
  5.                 ActivityInfo info = context.getPackageManager().getReceiverInfo(cn,  
  6.                         PackageManager.GET_META_DATA);  
  7.             } catch (NameNotFoundException e) {  
  8.                 e.printStackTrace();  
  9.             }  
  10.             String msg = info.metaData.getString("receiver_name");  
  11.             // 打电话测试即可  
  12.             Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();  
  13.         }  

你可能感兴趣的:(Android 用meta-data配置参数)