关于SharedPreference的封装,文件存储之共享参数,通过这个可以类似的实现对某个标志进行存储,比如说qq的记住密码,或者尽在wifi的情况下进行同步(最近做的就是这个)下面先是自己写了个封装的util单例类
/**
* Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hckj.caseapp.util;
import android.content.Context;
import android.content.SharedPreferences;
public class PreferenceUtils {
/**
* 保存Preference的name
*/
public static final String PREFERENCE_NAME = "caseApp";//要存储在这个名字的.xml下
private static SharedPreferences mSharedPreferences;
private static PreferenceUtils mPreferenceUtils;
private static SharedPreferences.Editor editor;
/** wifi才可进行同步,true:仅wifi下才可同步,false:2G3G4Gwifi有网便可同步 */
private String OnlyUnderTheWifiAutomaticSynchronization = "OnlyUnderTheWifiAutomaticSynchronization";
//构造参数做的工作室:通过context去得到getSharedPreferences
private PreferenceUtils(Context cxt) {
//系统会保存到一个名为caseApp.xml的文件中去,这个文件存到了应用程序中
mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
}
/**
* 单例模式,获取instance实例
*
* @param cxt
* @return
*/
public static PreferenceUtils getInstance(Context cxt) {//通过它可以得到一个new PreferenceUtils()构造参数的对象机会(然后需要看一//下这个构造参数做了什么)和edit()
if (mPreferenceUtils == null) {
mPreferenceUtils = new PreferenceUtils(cxt);
}
editor = mSharedPreferences.edit();
return mPreferenceUtils;
}
/** wifi才可进行同步,true:仅wifi下才可同步,false:2G3G4Gwifi有网便可同步 */
public void setSettingMsgSynchronization(boolean paramBoolean) {
editor.putBoolean(OnlyUnderTheWifiAutomaticSynchronization,
paramBoolean);
editor.commit();
}
/** wifi才可进行同步,true:仅wifi下才可同步,false:2G3G4Gwifi有网便可同步 */
public boolean getSettingMsgSynchronization() {
return mSharedPreferences.getBoolean(
OnlyUnderTheWifiAutomaticSynchronization, true);
}
}
//关于得到手机联网的状态,是wifi环境还是流量环境
package com.hckj.caseapp.tools;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class GetInternetType {
private Context context;
public GetInternetType(Context context) {
super();
this.context = context;
}
public static String getType(Context context) {
// 获取网络连接管理者
ConnectivityManager mConnectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// 检查网络连接
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
/**这个地方有个问题,就是能判断出是wifi环境还是其他环境,在这个地方自己测试的时候手机流量时是返回other*/
if (info == null || !mConnectivity.getBackgroundDataSetting()) {
return "nothing";
} else {
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) { // WIFI
return "wifi";
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) { // MOBILE
return "mobile";
} else {
return "other";
}
}
}
}
//用法:
先判断是否设置了在wifi下进行某项操作
boolean isWifi = PreferenceUtils.getInstance(ScanService.this)
.getSettingMsgSynchronization();//true则表示是false标识否
后去得到手机联网状态
String type = GetInternetType.getType(ScanService.this);//返回nothing则未联网,wifi则是wifi环境,other则是手机流量