Android - 讀取Properties設定檔

來源:互聯網
上載者:User

標籤:

  在Android中讀取設定檔,可以使用System.getProperties()方法讀取:

    1,在res資來源目錄下,建立一個檔案夾 raw,然後在其下建立一個.properties檔案.如:

request_char=utf-8URL=http://192.168.1.101:8080/ServerAQI/JsonActionrange_long=7#daysfrom_date_name=fromDateto_date_name=toDate

     2,可以定義一個工具類,接受android.content.res.Resources類型的參數,返回Properties對象,如:

package spt.assist;import java.io.IOException;import java.util.Properties;import android.content.res.Resources.NotFoundException;import android.util.Log;import spt.aqi.activity.R;public class PropertyConfig {/**擷取設定檔資訊中的指定值. * @param resources * @param key * @return */public static String getProperty(android.content.res.Resources resources,String key) {Properties properties = getProperties(resources);return properties.getProperty(key);}/**擷取設定檔中的資訊. * @param resources * @return */public static Properties getProperties(android.content.res.Resources resources) {Properties props = new Properties();try {props.load(resources.openRawResource(R.raw.properties));} catch (NotFoundException e) {Log.i("sysout","ResourceSearcher:OpenFileFromUtil:" + e.getMessage());e.printStackTrace();return null;} catch (IOException e) {Log.i("sysout","ResourceSearcher:OpenFileFromUtil:" + e.getMessage());e.printStackTrace();return null;}return props;}}

     3,在Android中的資源類ContextWrapper的子類(如Activity或Service)類中調用調用getResources()方法並傳入上面的工具類的方法,如,在Service類中,

final String url = PropertyConfig.getProperty(getResources(), "URL");

 

Android - 讀取Properties設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.