android设置系统时间
2016-05-17 14:30
441 查看
Android 通过应用设置系统日期和时间的方法
android 2.3
android 4.0
测试可行,不过需要ROOT权限.
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
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;
}
}
android 2.3
android 4.0
测试可行,不过需要ROOT权限.
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
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;
}
}
相关文章推荐
- android 基础 线程sleep,join,yield
- view--4种Android获取View宽高的方式
- git开源库——调试
- android保存图片到本地并可以在相册中显示出来
- 【Android】关于addTextChangedListener()方法的上机记录
- git开源库——视图控件——viewpager
- Android--SurfaceView使用详解
- git开源库——视图控件——左侧抽屉侧边栏
- Android Telephony 框架分析
- Android 音频开发-如何存储和解析wav文件
- git开源库汇总——动画篇
- Android程序中如何启动浏览器
- 关于Android MVP设计模式
- 彻底理解android中的内部存储与外部存储
- android 下载工具
- android studio怎么快速打出System.out.println()
- GitHub 优秀的 Android 开源项目
- 怎么向Android stutio中导入第三方.jar包
- android webView使用js/css实现夜间模式 长按识别图片以及二维码,网页可以上传图片
- com.orhanobut.logger开源框架查看日志