Android through the app to set the system's date and time in the

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.