看过其他的方法,感觉不方便,现贴出我的处理方法:
步骤为三步:
1、应用首先要申请获取root权限。代码如下:
/**
* 请求root权限
* @return 应用程序是/否获取Root权限
*/
public static boolean requestRootPermission(String pkgCodePath) {
Process process = null;
DataOutputStream os = null;
try {
String cmd="chmod 777 " + pkgCodePath;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
注:pkgCodePath可以使用context的getPackageCodePath()方法获取。2、设置时区。代码如下:
AlarmManager mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setTimeZone("GMT+09:00");
注:需要在AndroidManifest.xml文件中注明如下权限:<uses-permission android:name="android.permission.SET_TIME_ZONE"/>
3、成功获取root权限后,并设置好时区后,便可进行时间设置。代码如下:
/**
* 设置系统时间
* @param time 格式为“年月日.时分秒”,例如:20111209.121212
*/
public static boolean setTime(String time) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("date -s " + time + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
So, that's cool!