• Android 从AndroidManifest获取meta-data


    语法如下:

    <meta-data android:name="string"
               android:resource="resource specification"
               android:value="string" />
    

    <meta-data>标签可以作为子标签,可以被包含在<activity>、<application> 、<service>和<receiver>中:

    1. Activity标签中:

     <activity...>
           <meta-data android:name="Data_Name" android:value="DataValue"></meta-data>
        </activity>
    

     在代码中动态获取:

    ActivityInfo info=this.getPackageManager()
                              .getActivityInfo(getComponentName(),
                              PackageManager.GET_META_DATA);
        String msg =info.metaData.getString("Data_Name");

    2.application标签中:

    <application...>
           <meta-data android:value="DataValue" android:name="Data_Name"></meta-data>
       </application>
    

    在代码中动态获取:

     ApplicationInfo appInfo = this.getPackageManager()
                                      .getApplicationInfo(getPackageName(),
                              PackageManager.GET_META_DATA);
        String msg=appInfo.metaData.getString("Data_Name");
    

    3.在service标签中:

    <service android:name="MetaDataService">
          <meta-data android:value="DataValue" android:name="Data_Name"></meta-data>
       </service>
    

     在代码中获取:

    ComponentName cn=new ComponentName(this, MetaDataService.class);
       ServiceInfo info=this.getPackageManager()
                            .getServiceInfo(cn, PackageManager.GET_META_DATA);
        String msg=info.metaData.getString("Data_Name");
    

    4. 在receive标签中:

     <receiver android:name="MetaDataReceiver">
                <meta-data android:value="DataValuer" android:name="Data_Name"></meta-data>
                <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE"></action>
                </intent-filter>
        </receiver>
    

    在代码中获取:

    ComponentName cn=new ComponentName(context, MetaDataReceiver.class);
        ActivityInfo info=context.getPackageManager()
                                 .getReceiverInfo(cn, PackageManager.GET_META_DATA);
        String msg=info.metaData.getString("Data_Name");
    

       

  • 相关阅读:
    ansible
    爬虫框架之scrapy
    Mongodb
    xml 创建 添加节点或属性(自定义,复制)
    web站点崩溃的原因总结
    C# 关闭正在执行的文件
    254. Factor Combinations
    256. Paint House
    156. Binary Tree Upside Down
    170. Two Sum III
  • 原文地址:https://www.cnblogs.com/spring87/p/5810381.html
Copyright © 2020-2023  润新知