我需要以编程方式从Android设置日期/时间,但我没有成功!我有以上这三个来源:
源代码1
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.setTime(1330082817000);
Run Code Online (Sandbox Code Playgroud)
AndroidManifest:
<uses-permission android:name="android.permission.SET_TIME" />
<uses-permission android:name="android.permission.SET_TIME_ZONE" />
Run Code Online (Sandbox Code Playgroud)
例外:
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: …Run Code Online (Sandbox Code Playgroud) 背景
我正在使用三星Galaxy标签(7")的Wi-Fi版本进行一个项目.在某些设备上,我们看到设备上的日期/时间被重置(不知道为什么)到某个日期在过去(如2000年1月1日).
问题(S)
如何以编程方式重置设备日期/时间?如果可能的话,我更愿意使用一些adb shell命令(我的shell脚本生锈).如果没有可以实现此目的的shell命令,有没有办法通过编写和安装应用程序来实现这一点(似乎是安全违规)?
我只是在玩一些代码.我创建一个Activity,只需执行以下操作:
long lo = currentTimeMillis();
System.out.println(lo);
lo *= 3;
System.out.println(lo);
SystemClock.setCurrentTimeMillis(lo);
System.out.println( currentTimeMillis() );
Run Code Online (Sandbox Code Playgroud)
是的,在我的AndroidManifest.xml中,我添加了:
<uses-permission android:name="android.permission.SET_TIME"></uses-permission>
<uses-permission android:name="android.permission.SET_TIME_ZONE"></uses-permission>
Run Code Online (Sandbox Code Playgroud)
没有什么变化.SystemClock永远不会被重置......它只是继续滴答作响.我得到的错误只是表示权限"SET_TIME"未被授予该程序.保护等级3.
权限在那里......并且在2.2的API中它表示现在支持此功能.我不知道我做错了什么.
如果是android.content.Intent; 发挥作用,请解释.我真的不明白意图背后的想法是什么!
谢谢你的帮助!