Android 讀取Manifest檔案下Application 等節點下的meta-data自訂資料

來源:互聯網
上載者:User

Android 讀取Manifest檔案下Application 等節點下的meta-data自訂資料

介紹Android在提供了meta-date使用鍵值對的形式實現自訂配置。一般使用中作為渠道標識。可以用在,, , ,節點下。所有的值儲存在同一個Bundle裡面,可以使用 PackageItemInfo.metaData渠道這些資料。

使用形式:

resource 和 value直接用一個即可,但是注意如果使用

那麼得到的是ID值,也就是在R檔案裡面的數字,不是實際資源值。而要使用resource來取資源值(使用getInt()擷取)。


android:name:確保是唯一的就OK了。

android:value:值可以是:

1.字串 使用getString()擷取

2.整數 如“100” 使用getInt()擷取

3.布爾值:“true”,"false" 使用getBoolean()擷取

4.顏色值: "#rgb", "#argb", "#rrggbb", 或者 "#aarrggbb" 使用getInt()擷取

5.浮點值 :“1.2” 使用getFloat()

注意:Bundle中使用的是return (String) o;來擷取的代碼擷取一個StringValue值的,如果你配置的想配置的一個字串的值是“000”時,但是在將metadata封裝成bundle的時候,"000"被解析成整數0,所以使用getString()擷取的會是null。

執行個體:在Manifest節點裡最後加上:
 在自訂的Application裡面一個方法:
/** * 擷取Manifest裡面配置的渠道版本 * 

2014-11-14

* @return * @author RANDY.ZHANG */public String getHaiwanVersion() {String channel = "";try {channel = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("HAIWAN_CHANNEL");} catch (NameNotFoundException e) {e.printStackTrace();}return channel;}
其他節點:
//在Activity應用元素。    ActivityInfo info = this.getPackageManager()            .getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);    info.metaData.getString("meta_name");     //在application應用元素。    ApplicationInfo appInfo = this.getPackageManager()            .getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);    appInfo.metaData.getString("meta_name");     //在service應用元素。    ComponentName cn = new ComponentName(this, MetaDataService.class);    ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);    info.metaData.getString("meta_name");     //在receiver應用元素。    ComponentName cn = new ComponentName(context, MetaDataReceiver.class);    ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);    info.metaData.getString("meta_name");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.