Android 通過應用設定系統日期和時間的方法

來源:互聯網
上載者:User

Android 通過應用設定系統日期和時間的方法

android 2.3

android 4.0

測試可行,不過需要ROOT許可權.

 

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 setDateTime(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 > 1000)        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 > 1000)        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 > 1000)        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;}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.