在 Android 的 Mainfest 清单文件中,Application,Activity,Recriver,Service 的节点中都有这个的存在。很多时候我们可以通过 meta-data 来配置一些东西,例如我们可以配一个app相应的欢迎页面跳转后的主页等等。
下面直接上代码:
AndroidMainfest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="me.fwl.metadatademo"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <!--Application的MetaData的配置--> 12 <meta-data android:name="applicationMetadataKey" android:value="applicationMetadataValue"/> 13 <activity android:name=".MainActivity"> 14 <!--Activity的MetaData的配置--> 15 <meta-data android:name="activityMetadataKey" android:value="activityMetadataValue"/> 16 <intent-filter> 17 <action android:name="android.intent.action.MAIN" /> 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 22 <receiver android:name=".MainReceiver"> 23 <!--Receiver的MetaData的配置--> 24 <meta-data android:name="receiverMetadataKey" android:value="receiverMetadataValue"/> 25 </receiver> 26 <service android:name=".MainService"> 27 <!--Service的MetaData的配置--> 28 <meta-data android:name="serviceMetadataKey" android:value="serviceMetadataValue"/> 29 </service> 30 </application> 31 32 </manifest>
MainActivity.java
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 2 3 private TextView result; 4 private Context mContext; 5 private ComponentName cn; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 mContext = this; 12 cn = this.getComponentName(); 13 result = (TextView) findViewById(R.id.result); 14 findViewById(R.id.app).setOnClickListener(this); 15 findViewById(R.id.act).setOnClickListener(this); 16 findViewById(R.id.rec).setOnClickListener(this); 17 findViewById(R.id.ser).setOnClickListener(this); 18 } 19 20 @Override 21 public void onClick(View v) { 22 switch (v.getId()) { 23 case R.id.app: 24 try { 25 ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA); 26 String appMV = appInfo.metaData.getString("applicationMetadataKey"); 27 result.setText(appMV); 28 } catch (PackageManager.NameNotFoundException e) { 29 e.printStackTrace(); 30 } 31 break; 32 case R.id.act: 33 try { 34 ActivityInfo actInfo = mContext.getPackageManager().getActivityInfo(cn, PackageManager.GET_META_DATA); 35 String actMV = actInfo.metaData.getString("activityMetadataKey"); 36 result.setText(actMV); 37 } catch (PackageManager.NameNotFoundException e) { 38 e.printStackTrace(); 39 } 40 41 break; 42 case R.id.rec: 43 ComponentName recCn = new ComponentName(mContext, MainReceiver.class); 44 try { 45 ActivityInfo recInfo = mContext.getPackageManager().getReceiverInfo(recCn, PackageManager.GET_META_DATA); 46 String recMV = recInfo.metaData.getString("receiverMetadataKey"); 47 result.setText(recMV); 48 } catch (PackageManager.NameNotFoundException e) { 49 e.printStackTrace(); 50 } 51 break; 52 case R.id.ser: 53 ComponentName serCn = new ComponentName(mContext, MainService.class); 54 try { 55 ServiceInfo serInfo = mContext.getPackageManager().getServiceInfo(serCn, PackageManager.GET_META_DATA); 56 String serMV = serInfo.metaData.getString("serviceMetadataKey"); 57 result.setText(serMV); 58 } catch (PackageManager.NameNotFoundException e) { 59 e.printStackTrace(); 60 } 61 break; 62 } 63 } 64 }
至于MainActivity对应的布局文件,还有一个Service和一个Receiver的代码就不贴了,也没啥意义,都是空的,布局文件里面就一个Textview来展示结果,和4个按钮,没了。各自YY吧。
代码也就在这里了,其实换汤不换药,只是稍微有点区别罢了,仔细看看就懂了。
2017-1-5 16:39