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很简单,在 ...
随机推荐
- Android 入门(1)使用第三方控件
最近公司需要,准备开发扫描枪. 话不多说,直接开始 创建新项目 1 如果遇到 Error:Execution failed for task ':app:preDebugAndroidTestBuil ...
- CF908D New Year and Arbitrary Arrangement(期望Dp+数学)
题目大意:给你一个空字符串,你有\(\frac{pa}{pa+pb}\)的概率往字符串最后面加个\(a\),\(\frac{pb}{pa+pb}\)的概率往字符串最后面加个\(b\),当子序列\(ab ...
- [WC2007]剪刀石头布(最大流)
洛古 一句话题意:给定一张图,每两点之间有一条有向边或无向边,把所有无向边定向,使图中三元环个数尽量多 因为原图是一个完全图,假设图中任意三点都能构成三元环,那么途中三元环的个数为:\(\binom{ ...
- Redmine简易安装与系统优化
安装版本为bitnami-redmine-2.6.5-0 ,用的Bitnami的一键安装包 . 下载地址https://bitnami.com/stack/redmine/installer 简要安装 ...
- NEED TO DO
任务清单 计算几何 KDtree 容斥 后缀自动机套数据结构 FFT 四边形不等式/决策单调性优化 欧拉路 KM算法 BM算法 数论 min25筛 后缀数组 吉司机线段树 生成函数 ...
- 第六篇--Ubuntu画图软件
有时图片需要经过处理,下载一个pinta软件 sudo apt-get install pinta 安装后可能不知道位置,没关系,点击图片右键,选择打开软件为pinta就行了.
- Vue(小案例_vue+axios仿手机app)_首页(底部导航栏+轮播图+九宫格)
---恢复内容开始--- 一.前言 1.底部导航(两种做法) 2.轮播图 ...
- pandas的分布式执行框架之modin
Scale your pandas workflows by changing one line of code To use Modin, replace the pandas impo ...
- Maven 构建浏览器解析userAgent类
创建Maven项目 添加pom.xml 依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...
- ng-app&data-ng-app
来源stackoverflow 区别:在验证html5时,ng-app会抛出一个错误,而对带data-前缀的特性不会抛出.其它方面这两个属性一样.