<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dc.metadatasummary01">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--位于activity中-->
<meta-data android:name="in_main_activity" android:value="I am in activity"></meta-data>
</activity>
<service android:name=".DCTestMetaDataService">
<!--位于服务中-->
<meta-data android:name="in_service" android:value="I am in Service"></meta-data>
</service>
<receiver android:name=".DCTestMetaDataReceiver">
<!--位于广播接收者中-->
<intent-filter>
<action android:name="dc_test_meta_data_receiver"></action>
</intent-filter>
<meta-data android:name="in_receiver" android:value="I am in Receiver"></meta-data>
</receiver>
<!--位于application中-->
<meta-data android:name="in_application" android:value="I am in application"></meta-data>
</application>
</manifest>
ActivityInfo info = this.getPackageManager().getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
String msg = info.metaData.getString("in_main_activity");
ApplicationInfo appInfo = this.getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
String msg = appInfo.metaData.getString("in_application");
ComponentName cn = new ComponentName(this, DCTestMetaDataService.class);
ServiceInfo info2 = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
String msg = info2.metaData.getString("in_service");
ComponentName cn = new ComponentName(context, DCTestMetaDataReceiver.class);
ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
String msg = info.metaData.getString("in_receiver");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//拿取activity中的元数据
ActivityInfo info = null;
try {
info = this.getPackageManager()
.getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
String msg = info.metaData.getString("in_main_activity");
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//拿取application中的元数据
ApplicationInfo appInfo = null;
try {
appInfo = this.getPackageManager()
.getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String msg = appInfo.metaData.getString("in_application");
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//开启服务
startService(new Intent(MainActivity.this, DCTestMetaDataService.class));
//发送广播
Intent intent = new Intent();
intent.setAction("dc_test_meta_data_receiver");
sendBroadcast(intent);
}
@Override
protected void onDestroy() {
//关闭服务
stopService(new Intent(MainActivity.this, DCTestMetaDataService.class));
super.onDestroy();
}
/** * @author zyc * created at 2016/4/22 18:01 */
public class DCTestMetaDataReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (TextUtils.equals(intent.getAction(), "dc_test_meta_data_receiver")) {
ComponentName cn = new ComponentName(context, DCTestMetaDataReceiver.class);
ActivityInfo info = null;
try {
info = context.getPackageManager()
.getReceiverInfo(cn, PackageManager.GET_META_DATA);
String msg = info.metaData.getString("in_receiver");
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
/** * @author zyc * created at 2016/4/22 11:42 */
public class DCTestMetaDataService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//拿到meta-data元数据
ComponentName cn = new ComponentName(this, DCTestMetaDataService.class);
ServiceInfo info2 = null;
try {
info2 = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
String msg = info2.metaData.getString("in_service");
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
ok,so easy……