设置Android的日期/时间编程 [英] Set Android's date/time programmatically

查看:2231
本文介绍了设置Android的日期/时间编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置日期/时间从Android的编程,但我没有成功! 我有这三个来源上面:

I need set the date/time from Android programmatically, but I'm not having success! I have these three sources above:

来源$ C ​​$ C 1

Source code 1

AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);  
alarm.setTime(1330082817000); 

AndroidManifest:

AndroidManifest:

<uses-permission android:name="android.permission.SET_TIME" />  
<uses-permission android:name="android.permission.SET_TIME_ZONE" />  

例外:

Service fatal error : Unable to start activity ComponentInfo{br.com.tdta.service/br.com.tdta.service.Service}: java.lang.SecurityException: setTime: Neither user 10038 nor current process has android.permission.SET_TIME.  
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.tdta.service/br.com.tdta.service.Service}: java.lang.SecurityException: setTime: Neither user 10038 nor current process has android.permission.SET_TIME.  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)  
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)  
    at android.os.Handler.dispatchMessage(Handler.java:99)  
    at android.os.Looper.loop(Looper.java:123)  
    at android.app.ActivityThread.main(ActivityThread.java:4627)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:521)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)  
    at dalvik.system.NativeStart.main(Native Method)  
Caused by: java.lang.SecurityException: setTime: Neither user 10038 nor current process has android.permission.SET_TIME.  
    at android.os.Parcel.readException(Parcel.java:1247)  
    at android.os.Parcel.readException(Parcel.java:1235)  
    at android.app.IAlarmManager$Stub$Proxy.setTime(IAlarmManager.java:237)  
    at android.app.AlarmManager.setTime(AlarmManager.java:289)  
    at br.com.tdta.service.Service.onCreate(Service.java:32)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  
... 11 more  

====

====

来源$ C ​​$ C 2

Source code 2

boolean itsWork = SystemClock.setCurrentTimeMillis(1330082817000);  
System.out.println(itsWork);  

清单:

<uses-permission android:name="android.permission.SET_TIME" />  
<uses-permission android:name="android.permission.SET_TIME_ZONE" />  

itsWork 值:

false  

====

====

来源$ C ​​$ C 3

Source code 3

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd.hhmmss");
String data = format.format(new Date(1330082817000));
try {
    Runtime.getRuntime().exec("date -s " + data);
} catch (IOException e) {
}

我是什么做错了吗?

What am I doing wrong?

在此先感谢!

推荐答案

用户应用程序没有权限更改设备的时间。通过cashbash在<一个请阅读答案href="http://stackoverflow.com/questions/1332269/how-to-set-mobile-system-time-and-date-in-android">following帖子的替代选项。

The user application does not have permission to change the device time. Please read the answer by cashbash in the following post for the alternate option.

这篇关于设置Android的日期/时间编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆