android:value=“xiaoming” />
android:name
分配给该标签的键,即唯一名称。为确保名称唯一应使用Java风格命名约定,如“
com.example.project.activity.fred”。
android:resource
对资源的引用,如“@string/app_name”。该资源ID可以通过该metaData.getInt()方法获取 。
android:value
分配给该标签的值,如String、Boolean等。
在AndroidMainfest.xml中:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“me.fwl.metadatademo”>
<application
android:allowBackup=“true”
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl=“true”
android:theme="@style/AppTheme">
在mainActivity中:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView result;
private Context mContext;
private ComponentName cn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
cn = this.getComponentName();
result = (TextView) findViewById(R.id.result);
findViewById(R.id.app).setOnClickListener(this);
findViewById(R.id.act).setOnClickListener(this);
findViewById(R.id.rec).setOnClickListener(this);
findViewById(R.id.ser).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.app:
try {
ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
String appMV = appInfo.metaData.getString(“applicationMetadataKey”);
result.setText(appMV);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
break;
case R.id.act:
try {
ActivityInfo actInfo = mContext.getPackageManager().getActivityInfo(cn, PackageManager.GET_META_DATA);
String actMV = actInfo.metaData.getString(“activityMetadataKey”);
result.setText(actMV);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
break;
case R.id.rec:
ComponentName recCn = new ComponentName(mContext, MainReceiver.class);
try {
ActivityInfo recInfo = mContext.getPackageManager().getReceiverInfo(recCn, PackageManager.GET_META_DATA);
String recMV = recInfo.metaData.getString(“receiverMetadataKey”);
result.setText(recMV);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
break;
case R.id.ser:
ComponentName serCn = new ComponentName(mContext, MainService.class);
try {
ServiceInfo serInfo = mContext.getPackageManager().getServiceInfo(serCn, PackageManager.GET_META_DATA);
String serMV = ser
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
Info.metaData.getString(“serviceMetadataKey”);
result.setText(serMV);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
break;
}
}
}
运行结果:
05-22 21:21:04.624 14238-14238/com.fedming.notches D/fdm: meta_app : value from meta_app
05-22 21:21:04.625 14238-14238/com.fedming.notches D/fdm: meta_act_id : 2131427357, meta_act_string : Notche