在 Android 的 Mainfest 清单文件中,Application,Activity,Recriver,Service 的节点中都有这个的存在。很多时候我们可以通过 meta-data 来配置一些东西,例如我们可以配一个app相应的欢迎页面跳转后的主页等等。
下面直接上代码:
AndroidMainfest.xml
1 24 5 11 12 31 3213 14 15 21 2216 17 2018 19 23 24 2625 27 28 3029
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 @Override21 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