Android read/write Configuration 2

Source: Internet
Author: User

The previous article uses the Properties read-write configuration, various path errors, or no write permissions.

Later check the data, use another way to read and write Sharedpreferencesimpl

Direct Sticker Code

public class--Read and write

 Packagecom.lemon.demo.utils;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;ImportAndroid.util.Log; Public classUrlconfig {Private Staticsharedpreferences share; Private StaticString Configpath = "AppConfig";  Public Staticsharedpreferences getProperties (context context) {Try{Share=context.getsharedpreferences (Configpath, context.mode_private); } Catch(Exception e) {e.printstacktrace (); }        returnshare; }     Public Staticstring SetProperties (context context, string keyName, String keyValue) {Try{Share=context.getsharedpreferences (Configpath, context.mode_private); Sharedpreferences.editor Editor= Share.edit ();//Get editorEditor.putstring (KeyName, KeyValue);//The storage configuration parameter 1 is the key parameter 2 is the valueEditor.commit ();//Submit Refresh Data        } Catch(Exception e) {e.printstacktrace (); LOG.E ("Setpropertieserror", e.tostring ()); return"Failed to modify configuration file!"; }        return"Setup succeeded"; }}

Packaging

 PackageCom.lemon.demo.json;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;ImportCom.lemon.demo.utils.UrlConfig;/*** Network Request*/ Public classurlstring {    Private Static FinalString IP = "192.168.1.10:8000"; PrivateString contrastipname = "Contrastip"; //Upload Path    PrivateString IP; PrivateString ipAddress;  Public voidsetipaddress (Context context) {//Properties proper = properties.getproperties (context); //This.ip = Proper.getproperty (Contrastipname, "");Sharedpreferences proper =urlconfig.getproperties (context);  This. IP = proper.getstring (contrastipname, ""); //Set Default values        if( This. Ip.equals ("")){             This. IP =IP; }         This. ipAddress = "http://" + This. IP + "/v1.0/index.html"; }     Publicstring Setipaddress (context context, String KeyValue) {//String result = properties.setproperties (context, contrastipname, keyValue);String result =urlconfig.setproperties (context, contrastipname, keyValue);  This. IP =KeyValue;  This. ipAddress = "http://" + This. IP + "/v1.0/index.html"; returnresult; }     PublicString GetIP () {return  This. IP; }     PublicString getipaddress () {return  This. ipAddress; }}

How to use the same article.

Reference: HTTP://WWW.JIANSHU.COM/P/87024CE283C9

Reference: http://leequer.iteye.com/blog/654942

Android read/write Configuration 2

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.