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

http://blog.csdn.net/aikongmeng/article/details/26730737



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 + "\n");  
  103.             os.writeBytes("exit $?\n");  
  104.         } finally {  
  105.             if(os != null) {  
  106.                 try {  
  107.                     os.close();  
  108.                 } catch (IOException e) {  
  109.                 }  
  110.             }  
  111.         }  
  112.   
  113.         return process;  
  114.     }  
  115. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值