上篇文章采用 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的更多相关文章

  1. Android读写properties配置文件

    写这篇文章之前可以成功运行,文章后就各种找不到文件.所以并没有采用此种方式,后期完善.详见下篇解决方案. 配置文件读取很容易,修改需要注意权限,比如assets目录下就不允许修改. 配置文件的创建: ...

  2. Win10下Android studio配置

    Win10下Android studio配置 一.安装Android Studio的准备工作 1.下载好JDK,去官网上找一个下载下来 2.安装JDK.并配置环境变量.安装过程:本人将使用的是jdk- ...

  3. [Android] 环境配置之正式版Android Studio 1.0

    昨天看见 Android Studio 1.0 正式版本发布了:心里挺高兴的. 算是忠实用户了吧,从去年开发者大会一开始出现 AS 后就开始使用了:也是从那时开始就基本没有用过 Eclipse 了:一 ...

  4. Android studio配置Git

    Android studio配置Git 1.下载window 版git并安装:下载地址 2.Android Studio设置git插件:File->Setting->Version Con ...

  5. [Android] 环境配置之基础开发环境(SDK/Android Studio)(转)

    [Android] 环境配置之基础开发环境(SDK/Android Studio)   博客: blog.csdn.net/qiujuer 网站: www.qiujuer.net 开源库: Geniu ...

  6. Android Studio配置Git及Git文件状态说明

    Android Studio配置Git还是比较简单的,麻烦的是可能中间出现各种问题.如果你想了解或感兴趣,请往下看. 首先你得下载Git客户端,网址:http://git-scm.com/downlo ...

  7. Android 读写SD卡的文件

    今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...

  8. 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  ...

  9. Android Studio 配置SVN实现代码管理

    Refference From:http://iaiai.iteye.com/blog/2267346 一.Android Studio配置SVN Android Studio关联配置SVN很简单,在 ...

随机推荐

  1. springdata 动态查询 是用来查询的 仅提供查询功能

    springdata 动态查询 是用来查询的 仅提供查询功能

  2. android O 打开设置->声音->“点按时震动问题”

    主要原因是和导航栏和屏幕最下方3个按键的属性配置有关,因为在PhoneWindowManager中调用方法performHapticFeedbackLw(null, HapticFeedbackCon ...

  3. Ueditor注意的地方

    复制粘贴内容到编辑器上时,一些标签的属性会被过滤,在config.js里添加白名单配置项,例如: whitList: { a: ['target', 'href', 'title', 'class', ...

  4. BZOJ4977[Lydsy1708月赛]跳伞求生——贪心+堆+模拟费用流

    题目链接: 跳伞求生 可以将题目转化成数轴上有$n$个人和$m$个房子,坐标分别为$a_{i}$和$b_{i}$,每个人可以进一个他左边的房子,每个房子只能进一个人.每个房子有一个收益$c_{i}$, ...

  5. 天气预测(CNN)

    import torch import torch.nn as nn import torch.utils.data as Data import numpy as np import pymysql ...

  6. html5 基础入门

    html5 基础入门 前言介绍 HTML5草案的前身名为 Web Applications 1.0,于2004年被WHATWG提出,于2007年被W3C接纳,并成立了新的 HTML工作团队. 如果从狭 ...

  7. Kth MIN-MAX 反演

    MIN-MAX 反演 我们知道对于普通的 \(\min-\max\) 容斥有如下式子: \[ \max(S) = \sum_{T \subseteq S} (-1)^{|T| + 1} \min(T) ...

  8. PHP 加解密方法大全

    最近看见一篇文章讲的是PHP的加解密方法,正好也自己学习下,顺便以后有用到的地方也好能快速用上,仅供自己学习和复习,好了不多BB,上代码. 基于这几个函数可逆转的加密为:base64_encode() ...

  9. Pandas系列(十三)-其他常用功能

    一.统计数据频率 1. values_counts pd.value_counts(df.column_name) df.column_name.value_counts() Series.value ...

  10. module 'pip' has no attribute 'main'

    摘录自:http://www.cnblogs.com/Fordestiny/p/8901100.html 问题分析: 问题解决: 找到安装目录下 helpers/packaging_tool.py文件 ...