zoukankan      html  css  js  c++  java
  • android设置系统时间

    Android 通过应用设置系统日期和时间的方法

    android 2.3 

    android 4.0

    测试可行,不过需要ROOT权限.

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. import java.io.DataOutputStream;  
    2. import java.io.File;  
    3. import java.io.IOException;  
    4. import java.util.Calendar;  
    5.   
    6. import android.os.SystemClock;  
    7.   
    8. public class SystemDateTime {  
    9.       
    10.     static final String TAG = "SystemDateTime";   
    11.       
    12.     public static void setDateTime(int year, int month, int day, int hour, int minute) throws IOException, InterruptedException {  
    13.   
    14.         requestPermission();  
    15.   
    16.         Calendar c = Calendar.getInstance();  
    17.   
    18.         c.set(Calendar.YEAR, year);  
    19.         c.set(Calendar.MONTH, month-1);  
    20.         c.set(Calendar.DAY_OF_MONTH, day);  
    21.         c.set(Calendar.HOUR_OF_DAY, hour);  
    22.         c.set(Calendar.MINUTE, minute);  
    23.           
    24.           
    25.         long when = c.getTimeInMillis();  
    26.   
    27.         if (when / 1000 < Integer.MAX_VALUE) {  
    28.             SystemClock.setCurrentTimeMillis(when);  
    29.         }  
    30.   
    31.         long now = Calendar.getInstance().getTimeInMillis();  
    32.         //Log.d(TAG, "set tm="+when + ", now tm="+now);  
    33.   
    34.         if(now - when > 1000)  
    35.             throw new IOException("failed to set Date.");   
    36.           
    37.     }  
    38.   
    39.     public static void setDate(int year, int month, int day) throws IOException, InterruptedException {  
    40.   
    41.         requestPermission();  
    42.   
    43.         Calendar c = Calendar.getInstance();  
    44.   
    45.         c.set(Calendar.YEAR, year);  
    46.         c.set(Calendar.MONTH, month);  
    47.         c.set(Calendar.DAY_OF_MONTH, day);  
    48.         long when = c.getTimeInMillis();  
    49.   
    50.         if (when / 1000 < Integer.MAX_VALUE) {  
    51.             SystemClock.setCurrentTimeMillis(when);  
    52.         }  
    53.   
    54.         long now = Calendar.getInstance().getTimeInMillis();  
    55.         //Log.d(TAG, "set tm="+when + ", now tm="+now);  
    56.   
    57.         if(now - when > 1000)  
    58.             throw new IOException("failed to set Date.");  
    59.     }  
    60.   
    61.     public static void setTime(int hour, int minute) throws IOException, InterruptedException {  
    62.           
    63.         requestPermission();  
    64.   
    65.         Calendar c = Calendar.getInstance();  
    66.   
    67.         c.set(Calendar.HOUR_OF_DAY, hour);  
    68.         c.set(Calendar.MINUTE, minute);  
    69.         long when = c.getTimeInMillis();  
    70.   
    71.         if (when / 1000 < Integer.MAX_VALUE) {  
    72.             SystemClock.setCurrentTimeMillis(when);  
    73.         }  
    74.   
    75.         long now = Calendar.getInstance().getTimeInMillis();  
    76.         //Log.d(TAG, "set tm="+when + ", now tm="+now);  
    77.   
    78.         if(now - when > 1000)  
    79.             throw new IOException("failed to set Time.");  
    80.     }  
    81.       
    82.     static void requestPermission() throws InterruptedException, IOException {  
    83.         createSuProcess("chmod 666 /dev/alarm").waitFor();  
    84.     }  
    85.       
    86.     static Process createSuProcess() throws IOException  {  
    87.         File rootUser = new File("/system/xbin/ru");  
    88.         if(rootUser.exists()) {  
    89.             return Runtime.getRuntime().exec(rootUser.getAbsolutePath());  
    90.         } else {  
    91.             return Runtime.getRuntime().exec("su");  
    92.         }  
    93.     }  
    94.       
    95.     static Process createSuProcess(String cmd) throws IOException {  
    96.   
    97.         DataOutputStream os = null;  
    98.         Process process = createSuProcess();  
    99.   
    100.         try {  
    101.             os = new DataOutputStream(process.getOutputStream());  
    102.             os.writeBytes(cmd + " ");  
    103.             os.writeBytes("exit ");  
    104.         } finally {  
    105.             if(os != null) {  
    106.                 try {  
    107.                     os.close();  
    108.                 } catch (IOException e) {  
    109.                 }  
    110.             }  
    111.         }  
    112.   
    113.         return process;  
    114.     }  
    115. }  
  • 相关阅读:
    斐波那契数列相关
    社论CF1616G
    题解AGC056
    IOI2018 meetings
    题解UOJ#696. 【候选队互测2022】理论复杂度
    larval5.1模型静态使用多次出现查询属性信息存在问题
    SQL Server里面可能经常会用到的日期格式转换方法
    asp.net页面刷新后样式就发生了改变
    [武汉站]Windows 7 社区发布活动
    C++/CLI学习入门数组
  • 原文地址:https://www.cnblogs.com/vegetate/p/9997324.html
Copyright © 2011-2022 走看看