Android-Read the properties configuration file

Source: Internet
Author: User

To read the configuration file in Android, you can read it using the System.getproperties () method:

1, under the RES resource directory, create a new folder, Raw, and under which a. properties file is created. For example:

Request_char=utf-8url=http://192.168.1.101:8080/serveraqi/jsonactionrange_long=7#daysfrom_date_name=fromdateto _date_name=todate

2, you can define a tool class that accepts parameters of the Android.content.res.Resources type and returns the Properties object, such as:

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 {/** Gets the specified value in the profile information. * @param resources * @param key * @return */public Stati C String GetProperty (android.content.res.Resources resources,string key) {Properties properties = getProperties ( resources); return Properties.getproperty (key);} /** gets the information in the configuration file. * @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, call the Getresources () method in the subclass of the resource class contextwrapper (such as the activity or service) class in Android, and pass in the method of the tool class above, for example, in the service class,

Final String url = propertyconfig.getproperty (Getresources (), "url");

Android-Read the properties configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.