Android – Method for obtaining network time, specific time zone time and time synchronization

Recently, I sorted out Android - Methods to obtain network time, specific time zone time and time synchronization. The details are as follows:

Method 1:

The result obtained by this method is 24-hour system, and the month is also correct.

This method does not change with the time of the mobile phone. In other words, even if the mobile phone is set to another time zone, not Dongba District, the return of this method is still Beijing time!!! This is exactly the method I used when doing the project!! Completely solve the project needs!

Method 2:

Whether the results obtained by this method are in 24-hour system and the month is correct.

Method 3:

Method call:

It can also be seen from the code that a 1,24-hour system is added to the month

The above three methods are verified as follows:

The results are as follows:

I use the first method, because the time of the first method does not change with the time setting of the mobile phone.

The second method is also possible, but the time hour is 12 decimal.

The fourth method:

There is also this way to get time online:

In this way, a thread needs to be started to obtain time. At the same time, there is a risk that the response cannot be obtained due to network problems. Another important problem is that the acquisition of this time will change with the change of mobile phone time zone.

Method 5:

Through the network or GPS.

code:

Or real-time acquisition time:

When we use requestlocationupdates, we need to implement the locationlistener interface.

Get the time in the callback onlocationchanged of locationlisten

This method uses the mobile phone positioning system to obtain the time, but this method will change with the change of the time zone set by the mobile phone.

In the fourth and fifth methods, I have verified on the mobile terminal and can obtain the time normally. There is no problem with the way.

Since there are so many methods, we look at which method is suitable for our own needs and choose the corresponding method.

I still recommend the first method! Simple and practical^_^

Today, I will add some content on the basis of this article. Now I have such a demand in my project: when playing video, especially surveillance video, I want to have a time limit. I can watch it within a certain time. I can't watch it beyond the set time.

For this demand on the Android side, the above methods do not work, because the above methods are related to the mobile phone system. When I change the time and time zone of the mobile phone system, the obtained time will not correspond to the server time. What shall I do? There is a way!!!

I do this. When I get information, such as video node information, the server returns the current time of the server. With the time of the server, I use the time interval of user operation and the time of the server. Isn't that the current time of the mobile phone?

What about the time interval? See the following systemclock class methods:

1. System. Currenttimemillis() is a standard "wall" clock (time and date) that represents the number of milliseconds from the era to the present. The wall clock can be set by the user or telephone network (see set current time millis (long)), so the time may jump forward or backward unpredictably. The clock should only be used when the corresponding date and time in the real world are important, such as a calendar or alarm clock application. The interval time and elapsed time should use different clocks. If you use system. Currenttimemillis(), you can consider listening to action as action_ TIME_ TICK、 ACTION_ TIME_ CHANGED、ACTION_ TIMEZONE_ Changed broadcasts to monitor time changes.

2. Uptimemillis() indicates the count since system startup, in milliseconds. Returns the time in the non sleep period from the system startup to the present. When the system enters deep sleep (the CPU is turned off, the device turns black, waiting for external input devices), the clock will stop. However, the clock will not be affected by clock adjustment, idle or other energy-saving machines. This is the basic point for most intervals, such as thread. Sleep (mills), object. Wait (millis), and system. Nanotime (). The clock is guaranteed to be monotonic and is suitable for detecting intervals that do not include sleep. Most methods accept a timestamp value except uptimemillis () clock.

3. Elapsedrealtime() and elapsedrealtimenanos() return the time from system startup to the present, including the time of deep sleep of the device. The clock is guaranteed to be monotonous, and the time will continue to count even if the CPU is in power saving mode. The clock can be used when the measurement time interval may span the time period of system sleep.

There are several mechanisms to control when events occur:

1. Standard methods such as thread. Sleep (millis) and object. Wait (millis) are always available. These methods use the uptimemillis () clock. If the device enters deep sleep, the remaining time will be delayed until the system wakes up. These synchronization methods may be interrupted by thread. Interrupt(), and you must handle the interruptedexception exception.

2. Systemclock. Sleep (millis) is a practical method similar to thread. Sleep (millis), but it ignores the interruptedexception exception. If you don't use thread. Interrupt(), it will save the interrupt state of the thread.

3. The handler can set asynchronous callbacks at a relative or absolute time. The handler class object also uses the uptimemillis () clock and needs a loop (which often appears in Gui programs).

4. AlarmManager can trigger a single or repeated event even if the device is deeply dormant or the application is not running. The event can be set with currenttimemillis or elapsed realtime() (elapsed_realtime). A broadcast will be triggered when the event occurs.

method:

1. Public static long currentthreadtimemillis() returns the number of milliseconds running on the current thread.

2. Public static long elapsedrealtime() returns the number of milliseconds since the system was started, including the sleep time.

3. Public static long elapsedrealtimenanos() returns the number of nanoseconds since the system was started, including the sleep time.

4. Public static Boolean setcurrenttimemillis (long millis) sets the current "wall" time and requires the calling process to have permission. Returns whether it was successful.

5. Public static void sleep (long ms) waits for a given time. Similar to thread. Sleep (millis), but it does not throw an interruptedexception exception. The event is deferred to the next interrupt operation. The method does not return until the specified time has elapsed.

6. Public static long uptimemillis() returns the number of milliseconds since the system was started, excluding the sleep time. That is, count the time from system startup to the current non sleep period.

You can see these methods.

The rest is to calculate the event interval:

Public static long elapsedrealtime() returns the number of milliseconds since the system was started, including the sleep time

This method can calculate the time interval from the return time of the server to the time when the user operates and plays the video. With this, you can make the time not limited by the mobile phone system?

In the above method, the TS parameter is the current time returned by the server, such as the string intervaltime parameter of yyyy MM DD HH: mm: SS. It is the time interval from the server response to the user's operation. It is obtained by using the elapsedrealtime() method above to obtain the time twice.

Method, such as HH: mm: SS, so that the returned string is convenient for comparing time characters. Due to the limitation of video events, i.e. hours, minutes and seconds, it is impossible to involve year, month and day, so here is the direct return of hours, minutes and seconds.

With this method, it perfectly solves the need for android app to keep basically the same time as the server. However, there are still some problems, because the return time of the server is uncertain, which may be long or short, so the time will not be the same as the current time of the server. There will always be a delay, but this delay is acceptable, about 10 seconds at most. You can add a little bit to the time interval. Basically meet the needs.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>