android中meta-data的基本用法

有时候项目是在写不出来,就想学点新的知识找点感觉。

我也是网上看到的例子,只是自己去测试了下:

说明:一般的值可以通过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代码中:

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"));
}
(2)Application中:

<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代码中:

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"));
}
(3)Server中:

新建一个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代码中:

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"));
}
(4)Reciver中

清单文件中

<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"));
}
这很程序猿

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值