Android读写配置2
上篇文章采用 Properties 读写配置,各种路径错误,要么没有写入权限.
后来查资料,采用另一种方式读写 SharedPreferencesImpl
直接贴代码
公共类 -- 读写
package com.**.demo.utils; import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log; public class UrlConfig {
private static SharedPreferences share;
private static String configPath = "appConfig"; public static SharedPreferences getProperties(Context context) {
try {
share = context.getSharedPreferences(configPath, Context.MODE_PRIVATE); } catch (Exception e) {
e.printStackTrace(); } return share;
} public static String setProperties(Context context, String keyName, String keyValue) {
try {
share = context.getSharedPreferences(configPath, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = share.edit();//取得编辑器
editor.putString(keyName, keyValue);//存储配置 参数1 是key 参数2 是值
editor.commit();//提交刷新数据 } catch (Exception e) {
e.printStackTrace();
Log.e("setPropertiesError", e.toString());
return "修改配置文件失败!";
}
return "设置成功";
} }
封装
package com.**.demo.json; import android.content.Context;
import android.content.SharedPreferences;
import com.**.demo.utils.UrlConfig; /**
* 网络请求
*/ public class UrlString { private static final String IP = "192.168.1.10:8000"; private String contrastIPName = "contrastIP"; // 上传路径
private String ip;
private String ipAddress; public void setIPAddress(Context context) {
//Properties proper = ProperTies.getProperties(context);
//this.ip = proper.getProperty(contrastIPName, "");
SharedPreferences proper = UrlConfig.getProperties(context);
this.ip = proper.getString(contrastIPName, "");
// 设置默认值
if (this.ip.equals("")){
this.ip = IP;
}
this.ipAddress = "http://" + this.ip + "/v1.0/index.html";
} public String 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";
return result;
} public String getIP() {
return this.ip;
} public String getIPAddress() {
return this.ipAddress;
}
}
使用方式同上一篇.
参考: http://www.jianshu.com/p/87024ce283c9
参考: http://leequer.iteye.com/blog/654942
Android读写配置2的更多相关文章
- Android读写properties配置文件
写这篇文章之前可以成功运行,文章后就各种找不到文件.所以并没有采用此种方式,后期完善.详见下篇解决方案. 配置文件读取很容易,修改需要注意权限,比如assets目录下就不允许修改. 配置文件的创建: ...
- Win10下Android studio配置
Win10下Android studio配置 一.安装Android Studio的准备工作 1.下载好JDK,去官网上找一个下载下来 2.安装JDK.并配置环境变量.安装过程:本人将使用的是jdk- ...
- [Android] 环境配置之正式版Android Studio 1.0
昨天看见 Android Studio 1.0 正式版本发布了:心里挺高兴的. 算是忠实用户了吧,从去年开发者大会一开始出现 AS 后就开始使用了:也是从那时开始就基本没有用过 Eclipse 了:一 ...
- Android studio配置Git
Android studio配置Git 1.下载window 版git并安装:下载地址 2.Android Studio设置git插件:File->Setting->Version Con ...
- [Android] 环境配置之基础开发环境(SDK/Android Studio)(转)
[Android] 环境配置之基础开发环境(SDK/Android Studio) 博客: blog.csdn.net/qiujuer 网站: www.qiujuer.net 开源库: Geniu ...
- Android Studio配置Git及Git文件状态说明
Android Studio配置Git还是比较简单的,麻烦的是可能中间出现各种问题.如果你想了解或感兴趣,请往下看. 首先你得下载Git客户端,网址:http://git-scm.com/downlo ...
- Android 读写SD卡的文件
今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...
- Eclipse项目导入Android Stuio 配置出现 Timeout waiting to lock buildscript class cache for build file 'H:\studioproject\Generic_SN\build.gradle'
Eclipse项目导入Android Stuio 配置出现 Error:Timeout waiting to lock buildscript class cache for build file ...
- Android Studio 配置SVN实现代码管理
Refference From:http://iaiai.iteye.com/blog/2267346 一.Android Studio配置SVN Android Studio关联配置SVN很简单,在 ...
随机推荐
- Fixing “Did you mean to run dotnet SDK commands?” error when running dotnet –version
I recently installed the dotnet 1.11.0 Windows Server Hosting package which apparently installs the ...
- CODEVS 3546 矩阵链乘法
http://codevs.cn/problem/3546/ 题目 给定有n个要相乘的矩阵构成的序列(链)<A1,A2,A3,.......,An>,要计算乘积A1A2.....An.一组 ...
- 4.1 socket
socket 背景概念 脑图结构 OSI 模型 socket 概念特性 脑图结构 理解示意图 额外补充 Socket是应用层与 TCP/IP协议族通信的中间软件抽象层,它是一组接口. 在设计模式中 ...
- BSGS及扩展BSGS算法及例题
\(BSGS(baby-step-giant-step)\)算法是用来解高次同余方程的最小非负整数解的算法,即形如这个的方程: \(a^x\equiv b(mod\ p)\) 其中\(p\)为质数(其 ...
- Mycat的读写分离
1. Mycat实现读写分离的部署: https://www.cnblogs.com/softidea/p/5447566.html springboot动态数据源的原理以及配置: Spring内置了 ...
- Cygwin添加到鼠标右键
From:http://www.cnblogs.com/killerlegend/p/3960112.html 在cygwin中安装chere 管理员权限执行mintty,输入下列命令即可. cher ...
- mysql MHA高可用测试
[环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 [测试步骤:自动切换] 当前数据库状态: 系统 IP 主机名 备注 ...
- C#利用Vini.cs操作INI文件
VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VINI文档地址:https://github.com/velscode ...
- MacOS下好用的截图软件snip
1 官网下载,腾讯出的 https://snip.qq.com/ 2 下一步下一步安装就好,然后设置一下自己喜欢的快捷键,我的是command + control+J,选择自己喜欢的或者默认都可以 3 ...
- 项目改bug期间总结
kotlin中使用labda 表到时注意参数,另外setOnClickListener() 和 setOnClickListener{} 差别很大的,使用要细心, 因为这个问题拿了大半天闷, 最后还是 ...