Android through the app to set the system's date and time in the
Android 2.3
Android 4.0
Test is possible, just need root permission.
Import Java.io.dataoutputstream;import java.io.file;import Java.io.ioexception;import Java.util.Calendar;import Android.os.systemclock;public class Systemdatetime {static final String TAG = "systemdatetime"; public static void SetDate Time (int year, int month, Int. day, int hour, int minute) throws IOException, interruptedexception {requestpermission (); Calendar C = calendar.getinstance (); C.set (Calendar.year, year); C.set (Calendar.month, month-1); C.set (Calendar.day_of_month, day); C.set (Calendar.hour_of_day, HOUR); C.set (Calendar.minute, MINUTE); Long when = C.gettimeinmillis (); if (when/1000 < Integer.max_value) {Systemclock.setcurrenttimemillis (when); } Long now = Calendar.getinstance (). Gettimeinmillis (); LOG.D (TAG, "set tm=" +when + ", now tm=" +now); if (Now-when >) throw new IOException ("failed to set Date."); }public static void setDate (int year, int month, int day) throws IOException, interruptedexception {requestpermission (); Calendar C = calendar.getinstance (); C.set (Calendar.year, year); C.set (Calendar.month, MONTH); C.set (Calendar.day_of_month, day); Long when = C.gettimeinmillis (); if (when/1000 < Integer.max_value) {Systemclock.setcurrenttimemillis (when); } Long now = Calendar.getinstance (). Gettimeinmillis (); LOG.D (TAG, "set tm=" +when + ", now tm=" +now); if (Now-when >) throw new IOException ("failed to set Date."); public static void SetTime (int hour, int minute) throws IOException, interruptedexception {requestpermission (); Calendar C = calendar.getinstance (); C.set (Calendar.hour_of_day, HOUR); C.set (Calendar.minute, MINUTE); Long when = C.gettimeinmillis (), if (when/1000 < Integer.max_value) {Systemclock.setcurrenttimemillis (when);} Long now = Calendar.getinstance (). Gettimeinmillis (); LOG.D (TAG, "set tm=" +when + ", now tm=" +now); if (Now-when >) throw new IOException ("Failed to set time."); static void Requestpermission () throws Interruptedexception, IOException {createsuprocess ("chmod 666/dev/alarm"). WaitFor ();} Static Process createsuprocess () throws IOException {file Rootuser = new File ("/system/xbin/ru"); if (Rootuser.exists ()) { Return Runtime.getruntime (). EXEC (Rootuser.getabsolutepath ());} else {return runtime.getruntime (). EXEC ("su");}} Static Process createsuprocess (String cmd) throws IOException {DataOutputStream os = null; Process process = createsuprocess (); try {os = new DataOutputStream (Process.getoutputstream ()); Os.writebytes (cmd + "\ n") ; Os.writebytes ("Exit $?\n");} finally {if (OS! = null) {try {os.close ()} catch (IOException e) {}}}return process;}}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Android through the app to set the system's date and time in the