13

I want to set system clock by SystemClock.setCurrentTimeMillis, but met following error:

"Unable to open alarm driver: Permission denied"

Below is the Description of setCurrentMillis API from Android Developers Site:

public static boolean setCurrentTimeMillis (long millis) 

Since: API Level 1 Sets the current wall time, in milliseconds. Requires the calling process to have appropriate permissions.

Returns if the clock was successfully set to the specified time.

So, Application Needs to obtain the Permission to set the time. In AndroidManisfest.xml I inserted android.permission.SET_TIME but I have the same message error.

I have tried various solutions found in the internet and it seems that it's not possible to set system time without root even on FroYo (Android 2.2) where android.permission.SET_TIME was introduced, normal application can't get this permission because /dev/alarm file has 664 permission. So the solution is to change permission in 666 but require rooted device and is too invasive to be a solution.

I want to create an App for my Company to synchronize data between different systems and is essential to have synchronized clocks.

Thank you in advance for your willingness

Eclipses

Hussain
  • 5,552
  • 4
  • 40
  • 50
Eclipses
  • 1,008
  • 1
  • 9
  • 17
  • "I want to create an App for my Company to synchronize data between different systems and is essential to have synchronized clocks." -- if your server is dependent upon client-supplied time, fix the server, because it is buggy. – CommonsWare Jul 20 '11 at 10:48
  • Hi, the problem is if the user change system date and time in his device. The server clock is OK. Tnx – Eclipses Jul 20 '11 at 12:45
  • You misunderstand. Your server should not be relying upon the client date and time being accurate. Only a fool writes a server that relies upon accurate timing data from clients. – CommonsWare Jul 20 '11 at 13:21
  • 2
    Excuse me, but maybe you have not understood my need. I'll give you an example: using my app will check that you execute a job. I want to know when you've done. Just when you're done you have to press a button and store the date and time of execution of work. Then I want that this data is communicated to a server. Now imagine that when you complete the job does not have connectivity, so I want to store the data and send it as soon as I have connectivity to the central server. Do you think I should consider that the work is done in the date and time in which arrives on the server? – Eclipses Jul 20 '11 at 13:39
  • My point is that any user can change their time to be whatever they want. Even if you have this permission, you cannot stop the user from changing their time. It sounds like what you really need, then, is to get the time yourself from a trusted source rather than from the system. There are SNTP clients for Java that you can try using on Android as a library, to get your time from `pool.ntp.org` or someplace, such as http://code.google.com/p/sntpjc/ – CommonsWare Jul 20 '11 at 13:43
  • The problem is not where to get the exact time but to ensure that the time is correct even in the absence of connectivity. All users can always change time on the device. My idea at the time is to synchronize the time when the application starts (required connectivity), then start on a thread that increase the time and when a job is done verify that the time is reliable by comparing with that of the thread in case of significant difference (what the user has modified the device), use the time to the thread that will be update at the first successful connection. – Eclipses Jul 20 '11 at 14:10
  • 3
    Your thread will not live long enough, because one way or another, Android will terminate your process. And, you do not need to set the system time. When you get the time from SNTP or your server, record `SystemClock.elapsedRealtime()`. When you wish to store a time, use `SystemClock.elapsedRealtime()` and the recorded value to determine the # of milliseconds since you got the SNTP time, and add those milliseconds to the SNTP time to get the current time in the SNTP timebase. This won't work if the user reboots their phone but should otherwise be reasonably stable. – CommonsWare Jul 20 '11 at 14:19
  • I must try your solution but seems to be right. If the user reboots their phone I don't worry becouse I said that connectivity is required to start my app. So I don't need of a rooted device. Tnx so much ;-) – Eclipses Jul 20 '11 at 14:34
  • @CommonsWare, do you have any idea of solution that will work even though users reboot? Thanks – Zennichimaro Apr 04 '14 at 05:04
  • @Zennichimaro: No, sorry. – CommonsWare Apr 04 '14 at 07:28

3 Answers3

12

Eclipses,

I did quite a bit of checking for you on this and found an open ticket with Google to fix the problem. Only thing is the tag has been open since Nov 8, 2009. Basically only the system can change the time even with the Permission. According to comments on the link below a developer did some research and noted that when the permission SET_TIME was created, it wasn't given the appropriate protection level in the OS (signatureorSystem v.s dangerous). As a result it doesn't do what it is supposed to do.

http://code.google.com/p/android/issues/detail?id=4581

Here's an explanation of the various protection levels that permissions can have. Currently according to the developer cite above SET_TIME has "signatureorSystem" when it should have "dangerous".

http://developer.android.com/guide/topics/manifest/permission-element.html#plevel

In short there is currently no way to change the time programatically without rooting the phone.

One final note is that if you want to go with Custom roms there is also an open ticket for Cyanogenmod to add the ability to their rom as well.

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=CyanogenMod+SET_TIME

Hope this helps some, George

George Baker
  • 1,207
  • 9
  • 13
  • Thanks George, before put this question I did all the necessary research and had already found the ticket. I have a feeling that the problem is known to all and that is due to a bug but no one has worked to solve it. Until its latest release of the SDK. I hope through this channel to be able to focus the problem in Google. When I develop on a platform I want to be able to do everything. – Eclipses Jul 18 '11 at 14:58
  • 1
    Eclipses. Thanks for the feedback. IT is definitely something that needs to be fixed. – George Baker Jul 19 '11 at 14:28
  • 1
    This is not a bug, it is working as intended. The permission is set to signatureOrSystem because that is what is desired, not because it is a mistake. – hackbod Jul 20 '11 at 05:43
  • The need is to change the time to align it to the server which will exchange data. Will you explain how you think I could do a professional to do this? I found two solutions, the first is to check the time difference and if it is wrong to ask the user to correct it by hand by opening the window to arrange the date and time (`new Intent(android.provider.Settings.ACTION_DATE_SETTINGS);`), the other is to automate this process, as I explained in my question. – Eclipses Jul 20 '11 at 07:43
  • 1
    I think that like the permission to access to the Internet when you install the app warns you that you are installing the app will connect to the Internet, should do the same permission to change the system date and time. – Eclipses Jul 20 '11 at 07:43
  • George, I found this Issue 4581 attachment that reinforces your thinking http://code.google.com/p/android/issues/attachmentText?id=4581&aid=1674341479997991317&name=0016-Change-the-protectionLevel-from-signatureOrSystem-to.patch&token=3377b5778498183f8adbb2291e2ce28b – Eclipses Jul 20 '11 at 10:19
  • Eclipses Thanks- Got a response from Google on the issue. It was in response to Issue 4581 which is closed now. Basically don't look for it anytime soon. Here's what they said. - It is by design that applications can not change the time. There are many subtle aspects of security that can rely on the current time, such as certificate expiration, license management, etc. We do not want to allow third party applications to globally disrupt the system in this way. – George Baker Jul 21 '11 at 22:21
  • Tnx @George, I can only accept Google's decision but I still think it is a mistake because the user can always change the date and time on the device. It is believed that the third party applications always want to create security problems, but in most cases it is not. Do you know if there is a way to request the automatic time update from code? – Eclipses Jul 22 '11 at 05:28
3

According to this post, SET_TIME is the correct permission but it will only work on a rooted phone.

adamcodes
  • 1,606
  • 13
  • 21
  • You confirm my solution. Do you think that maybe is a bug and is too invasive solution require rooted device? – Eclipses Jul 05 '11 at 16:50
  • Well there are many apps that require rooted phones.. It really depends on what you plan to do with the app and who is going to use it. You can't ask of all 'normal' people to root their phones. – Manuel Jul 13 '11 at 10:16
  • Of course not everyone is going to root their phone for a small task as setting system time. I just wanted to confirm that it is the correct permission, but the task seems unfeasible. – adamcodes Jul 13 '11 at 21:36
  • 4
    Correct it is not allowed for third party apps to do this. – hackbod Jul 18 '11 at 22:04
  • Correct what? Why is not allowed? This is not a solution. Read well the question. Please don't vote up only to take bounty. Tnx – Eclipses Jul 19 '11 at 07:40
  • Seems correct to me - you can't do what you are trying on unrooted phones. – David Snabel-Caunt Jul 19 '11 at 13:44
  • In my question I wrote "So the solution is to change permission in 666 but require rooted device and is too invasive to be a solution." – Eclipses Jul 19 '11 at 14:26
  • 1
    "Correct what? Why is not allowed?" Correct it is not allowed. Why is it not allowed? Because we don't want third party apps messing with the user's time. – hackbod Jul 20 '11 at 05:43
  • @hackbod, please read new comments so you can understand my need and know alternative solution from CommonsWare. I cannot accept an answer with the solution that I wrote in my question. Tnx – Eclipses Jul 20 '11 at 16:00
  • @Eclipses: Then start a new question, which brings together all the updates and new approaches you have taken. Hackbod correctly answered the original question, so she deserves the "accept". – Christopher Orr Jul 25 '11 at 20:18
1

we've done it. Not a big deal as long as you can get the SET_TIME pemission. See my answer to a similar question.

Regarding other answers stating that it's not possible without rooting the phone then this refers only to gaining the permission. For this see here and here.

Community
  • 1
  • 1
jek
  • 2,406
  • 1
  • 18
  • 27