android设置系统时间

来源:互联网 发布:ni系列卸载软件 编辑:程序博客网 时间:2024/05/01 15:53
?**
*TODO 使用ADB方式设置系统时间
*/
public void setSystemTimeForAdb(Long millis) {
SimpleDateFormat df1 = new SimpleDateFormat("yyyymmdd.hhmmss");
Date setDate = new Date(millis);
String command  = "date -s " + df1.format(setDate);
execWithSID(new String[]{command});
}

/**
*TODO执行命令
*/
private static String execWithSID(String[] args) {
String result = null;
Process process = null;
OutputStream out = null;
try {
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
for (String tmp : args) {
dataOutputStream.writeBytes(tmp);
}
dataOutputStream.flush();
dataOutputStream.close();
out.close();
BufferedReader buf = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;

while ((line = buf.readLine()) != null) {
NLog.d(TAG, "in execWithSID, line=" + line);
result += line;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

是不是很简单?不需要在android源码下进行编译了
0 0