zoukankan      html  css  js  c++  java
  • Android 读取<meta-data>元素中的数据

    众所周知, 每个组件都有<meta-data>元素, 用于接收一些外部数据(eg: appKey), 那其中的值应该怎么读取呢. 

    1> Application

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <meta-data
                android:name="application_meta_data"
                android:value="application_meta_data_value" />  <!--可以关联资源ID, 支持多种数据类型 -->
    ApplicationInfo info = null;
            
         try {
             info = getPackageManager()
                       .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
         } catch (NameNotFoundException e) {
             e.printStackTrace();
         }
    Log.v(TAG, "application meta data value:" + info.metaData.getString("application_meta_data"));

    2> Activity 

    <activity
            android:name="fyc.app.testandroid.MainActivity" >
            <meta-data
                android:name="activity_meta_data"
                android:value="activity_meta_data_value" />
    </activity>
    ActivityInfo info = null;
            
        try {
            info = getPackageManager()
                     .getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    Log.v(TAG, "activity meta data value:" + info.metaData.getString("activity_meta_data"));

     3> Service

    <service android:name="fyc.app.testandroid.MyService" >
                <meta-data
                    android:name="service_meta_data"
                    android:value="service_meta_data_value" />
    </service>
    ServiceInfo info = null;
            
        try {
            info = getPackageManager()
                    .getServiceInfo(new ComponentName(this, MyService.class), PackageManager.GET_META_DATA);  //如果在当前组件获取直接使用getcomponentName()
        } catch (NameNotFoundException e) {
                e.printStackTrace();
        }
    Log.v(TAG, "service meta data value:" + info.metaData.getString("service_meta_data"));

     4> BroadcastReceiver

    <receiver android:name="fyc.app.testandroid.MyReceiver" >
            <meta-data
                    android:name="receiver_meta_data"
                    android:value="receiver_meta_data_value" />
    </receiver>
    ActivityInfo info = null;
            
        try {
            info = getPackageManager()
                    .getReceiverInfo(new ComponentName(this, MyReceiver.class), PackageManager.GET_META_DATA);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    Log.v(TAG, "receiver meta data value:" + info.metaData.getString("receiver_meta_data"));

     Logcat输出信息 

    11:11:46.733: V/MainActivity(571): application meta data value:application_meta_data_value
    11:11:46.733: V/MainActivity(571): activity meta data value:activity_meta_data_value
    11:11:46.763: V/MainActivity(571): service meta data value:service_meta_data_value
    11:11:46.783: V/MainActivity(571): receiver meta data value:receiver_meta_data_value

     官方介绍

    http://developer.android.com/guide/topics/manifest/meta-data-element.html

    欢迎分享转载,转载请注明出处 http://www.cnblogs.com/fangyucun
  • 相关阅读:
    手机网页中 唤醒拨打电话和发送短信,浏览器和微信浏览器都可以用
    用于滚动 焦点图的MSClass
    oracle使用connect by进行级联查询 树型菜单
    Jstl标签汇总
    EL表达式报错:  According to TLD or attribute directive in tag file, attribute value does not accept any expressions
    EL表达式简介
    SSM整合(四)-整合后配置文件汇总
    Maven的pom报maven-surefire-plugin:pom:2.12.4
    SSM整合(三):Spring4与Mybatis3与SpringMVC整合
    SSM整合(二):Spring4与Mybatis3整合
  • 原文地址:https://www.cnblogs.com/fangyucun/p/3309587.html
Copyright © 2011-2022 走看看