/**
* @return String time format with:yyyy_MMdd_HHmmss.
*//获取手机的第一次开机时间
*/
public static String getBootTimeString() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(getRebootTime());
String bootTimeStr = new
SimpleDateFormat("yyyy_MMdd_HHmmss").format(calendar.getTime());
Utils.logi(TAG, "bootTimeStr = " + bootTimeStr);
return bootTimeStr;
}
public static long getRebootTime() {
long rebootTime = new Date().getTime() - SystemClock.elapsedRealtime();
return new Date().getTime() - SystemClock.elapsedRealtime();
}
//计时器 mTimeMillisecond 为你的开始时间
mTimeMillisecond = SystemClock.elapsedRealtime() / 1000;
private String calculateTimer() {
if (mTimeMillisecond < 0) {
Utils.logi(TAG, "System time adjust to past, just reset log time to 0,"
+ " to avoid UI show incorrect");
mTimeMillisecond = 0;
}
int hour = (int) mTimeMillisecond / 3600;
if (hour > 9999) {
Utils.loge(TAG, "There is something wrong with time record! The hour is " + hour);
mTimeMillisecond = 0;
Utils.loge(TAG, "There is something wrong with time record!");
}
int minute = (int) mTimeMillisecond / 60 % 60;
long second = mTimeMillisecond % 60;
String timerStr =
"" + hour + ":" + (minute < 10 ? "0" : "") + minute + ":"
+ (second < 10 ? "0" : "") + second;
return timerStr;
}
//获取手机当前时间
public void getNowTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
android.util.Log.i("zcailiu","onHotplug time ="+ simpleDateFormat.format(date));
}
//判断某service是否运行
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = null;
try {
serviceList = activityManager.getRunningServices(100);
} catch (SecurityException re) {
return false;
}
if (!(serviceList.size() > 0)) {
return false;
}
for (int i = 0; i < serviceList.size(); i++) {
if (serviceList.get(i).service.getClassName().equals(className)) {
isRunning = true;
break;
}
}
Utils.logi(TAG + "/Utils",
"-->isServiceRunning() className = " + className + ", isRunning = " + isRunning);
return isRunning;
}
//java代码运行 adb 命令 并将返回命令的执行结果
//alps\vendor\mediatek\proprietary\packages\apps\MTKLogger\src\com\mediatek\mtklogger\debugtool\AEEControlService.java
private static StringBuffer systemexec(String command) {
StringBuffer output = new StringBuffer();
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = new String();
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
android.util.Log.i(TAG, output.toString());
process.waitFor();
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
android.util.Log.i(TAG, "Operation failed.");
} catch (InterruptedException ie) {
ie.printStackTrace();
android.util.Log.i(TAG, "Operation failed.");
}
return output;
}
//将一个long类型的时间转换同成日常时间格式
/**
* transfer long time to time string.
* @param time long type
* @return ex: 2012_1221_2359
*/
public static String translateTime2(long time) {
GregorianCalendar calendar = new GregorianCalendar();
DecimalFormat df = new DecimalFormat();
String pattern = "00";
df.applyPattern(pattern);
calendar.setTime(new Date(time));
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minu = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return "" + year + "_" + df.format(month) + df.format(day) + "_" + df.format(hour)
+ df.format(minu) + df.format(second);
}