对SharePreference的封装:
public class PrefUtils {
public static void putBoolean(String key, boolean value, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(String key, boolean defValue, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getBoolean(key, defValue);
}
public static void putString(String key, String value, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putString(key, value).commit();
}
public static String getString(String key, String defValue, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getString(key, defValue);
}
public static void putInt(String key, int value, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putInt(key, value).commit();
}
public static int getInt(String key, int defValue, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getInt(key, defValue);
}
public static void remove(String key, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().remove(key).commit();
}
}
ToastUtils的封装:
public class ToastUtils {
public static void showToast(Context ctx, String text) {
Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
}
}
MD5Utils的封装:
public class MD5Utils {
public static String encode(String password) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(password.getBytes());// 进行加密运算,返回加密后的字节数组
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
int i = b & 0xff;
String hexString = Integer.toHexString(i);
// System.out.println(hexString);
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
sb.append(hexString);
}
String md5 = sb.toString();
return md5;
} catch (NoSuchAlgorithmException e) {
// 没有此算法异常
e.printStackTrace();
}
return null;
}
/**
* 计算文件md5
*
* @param filePath
* @return
*/
public static String encodeFile(String filePath) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
FileInputStream in = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
digest.update(buffer, 0, len);
}
byte[] bytes = digest.digest();
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
int i = b & 0xff;
String hexString = Integer.toHexString(i);
// System.out.println(hexString);
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
sb.append(hexString);
}
String md5 = sb.toString();
return md5;
} catch (Exception e) {
// 没有此算法异常
e.printStackTrace();
}
return null;
}
}
StreamUtils的封装:
public class StreamUtils {
public static String stream2String(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();
return out.toString();
}
}
判断服务状态的ServiceStatusUtils工具类:
public class ServiceStatusUtils {
/**
* 判断服务是否正在运行
*
* @param serviceName
* @param ctx
* @return
*/
public static boolean isServiceRunning(String serviceName, Context ctx) {
// 活动管理器
ActivityManager am = (ActivityManager) ctx
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> runningServices = am.getRunningServices(100);// 获取运行的服务,参数表示最多返回的数量
for (RunningServiceInfo runningServiceInfo : runningServices) {
String className = runningServiceInfo.service.getClassName();
if (className.equals(serviceName)) {
return true;// 判断服务是否运行
}
}
return false;
}
}