有时候项目是在写不出来,就想学点新的知识找点感觉。
我也是网上看到的例子,只是自己去测试了下:
说明:一般的值可以通过value属性来指定,但是如果要指定一个资源的id,则需要使用resource属性来配置
(1)Activity中
在AndroidMainfest,xml中:
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="myMsgOfActivity" android:value="hello my activity"></meta-data> </activity>java代码中:
(2)Application中:private void getActivityData(){ ActivityInfo info= null; try { info = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Log.e("myMsgOfActivity",info.metaData.getString("myMsgOfActivity")); }
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:value="hello my application" android:name="myMsgOfApplication"></meta-data> //... </application>java代码中:
(3)Server中:private void getApplicationData(){ ApplicationInfo appInfo = null; try { appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Log.e("myMsgOfApplication",appInfo.metaData.getString("myMsgOfApplication")); }
新建一个Server类:
public class MyServer extends Service { @Override public IBinder onBind(Intent intent) { return null; } }
清单文件中:
<service android:name=".MyServer"> <meta-data android:value="hello my service" android:name="myMsgOfServer"></meta-data> </service>>java代码中:
(4)Reciver中private void getServerData(){ ComponentName cn=new ComponentName(this, MyServer.class); ServiceInfo info= null; try { info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Log.e("myMsgOfServer",info.metaData.getString("myMsgOfServer")); }
清单文件中
<receiver android:name=".MyBroadReceiver"> <meta-data android:value="hello my receiver" android:name="myMsgOfBroadReceiver"></meta-data> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"></action> </intent-filter> </receiver>新建一个广播
public class MyBroadReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { } }
java代码中
这很程序猿private void getBroadReciverData(){ ComponentName cn=new ComponentName(this, MyBroadReceiver.class); ActivityInfo info= null; try { info = this.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Log.e("myMsgOfBroadReceiver",info.metaData.getString("myMsgOfBroadReceiver")); }