Android中一些基本的工具类,如ToastUtils,PrefUtils,StreamUtils,MD5Utils...

对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;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值