Android应用开发实用方法小集
public static String getDataTime(String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(new Date());
}
/** 获取手机IMEI(移动设备国际身份码)码*/
public static String getPhoneIMEI(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getDeviceId();
}
/** 获取手机系统SDK版本*/
public static int getSDKVersion() {
return android.os.Build.VERSION.SDK_INT;
}
/*** 获取系统版本*/
public static String getSystemVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**调用系统发送短信*/
public static void sendSMS(Context context, String smsBody) {
Uri smsToUri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
intent.putExtra("sms_body", smsBody);
context.startActivity(intent);
}
/** 判断网络是否连接 */
public static boolean checkNet(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return info != null;//根据连接是否为null来判断网络是否连接
}
/**判断网络是否为wifi*/
public static boolean isWiFi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// wifi的状态:ConnectivityManager.TYPE_WIFI
// 3G的状态:ConnectivityManager.TYPE_MOBILE
State state = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
return State.CONNECTED == state;
}
/** 判断当前应用程序是否后台运行 */
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
// 后台运行
return true;
}
else {
// 前台运行
return false;
}
}
}
return false;
}
/** 判断手机是否处理睡眠 */
public static boolean isSleeping(Context context) {
KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();
return isSleeping;
}
/**安装apk */
public static void installApk(Context context, File file) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("application/vnd.android.package-archive");
intent.setData(Uri.fromFile(file));
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
/**模拟点击home键,后台运行*/
public static void goHome(Context context) {
Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
mHomeIntent.addCategory(Intent.CATEGORY_HOME);
mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(mHomeIntent);
}
/**获取应用签名*/
public static String getSign(Context context, String pkgName) {
try {
PackageInfo pis = context.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
return hexdigest(pis.signatures[0].toByteArray());
}
catch (NameNotFoundException e) {
throw new KJException(SystemTool.class.getName() + "the " + pkgName + "‘s application not found");
}
}
/**将签名字符串转换成需要的32位签名*/
private static String hexdigest(byte[] paramArrayOfByte) {
final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
char[] arrayOfChar = new char[32];
for (int i = 0, j = 0;; i++, j++) {
if (i >= 16) { return new String(arrayOfChar); }
int k = arrayOfByte[i];
arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
arrayOfChar[++j] = hexDigits[(k & 0xF)];
}
}
catch (Exception e) {
}
return "";
}
/** 获取设备的可用内存大小*/
public static int getDeviceUsableMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存(单位:M)
return (int) (mi.availMem / (1024 * 1024));
}
/** 清理后台进程与服务*/
public static int gc(Context context) {
long i = getDeviceUsableMemory(context);
int count = 0; // 清理掉的进程数
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 获取正在运行的service列表
List<RunningServiceInfo> serviceList = am.getRunningServices(100);
if (serviceList != null) for (RunningServiceInfo service : serviceList) {
if (service.pid == android.os.Process.myPid()) continue;
try {
android.os.Process.killProcess(service.pid);
count++;
}
catch (Exception e) {
e.getStackTrace();
continue;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。