Address: http://www.cnblogs.com/kross/p/3643708.html
AlarmManager can set a specified time, or repeat an operation from a specified time interval. It can be used to create an alarm program or access the Internet at a specific time.
The following code is used to obtain AlarmManager and other services of the system:
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
We use AlarmManager to send a request to BroadcastReceiver. First, we simply implement a BroadcastReceiver.
MyReceiver String TAG = "MyReceiver" "onReceive"
After completing this step,Remember to register this class in the Manifest FileOtherwise, there is no response ......
Then we get a button in MainActivity, and the Intent is created in the button method, so that AlarmManager can send the request.
"foo"= Intent(, MyReceiver."hahah"= PendingIntent.getBroadcast(, 1= time = am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 5000"foo over"
This is the most important thing.ElapsedRealtimeAfter my tests, I finally found out what this time was.
It is the current time after you start the instance. For example, if you turn off your phone and start the instance, The first thing after the instance is started is to call the SystemClock. elapsedRealtime () method, which is converted into a string. The obtained time is1970.01.01 08:00:00
Then your subsequent calls are based on this time, for example30 s after startupYou can call this method to obtain the time1970.01.01 08:00:30
If youRestartThe time will beResetBack1970.01.01 08:00:00.
I have read a lot of blogs that have not clearly explained this point. They are all vague. According to this article, I have translated the document and read it clearly.
AlarmManagerSetThe method is to send a request,SetRepeatingThe request is sent repeatedly. The first parameters of the two methods are very strange. I didn't really understand it.
In fact, it can be divided into two groups, one isRLAPSED_REALTIME,RLAPSED_REALTIME_WAKEUP, The other group isRTC,RTC_WAKEUPThe difference between the two groups is the time used, and the RLAPSED_REALTIME group uses SystemClock. elapsedRealtime () is the normal time for RTC. What is the difference between the two? The latter RTC allows users to modify the Time of the mobile phone (that is, set the time, can be changed by anyone), and may be repeatedly called to AlarmManager. Elapsed realtime cannot be used.
Then there is this WAKEUP problem. In this document, the device will be awakened during sleep, but I don't know what the so-called device sleep is ...... I made an example of sending a request within five seconds. After the request is started, I turn off the screen (that is, the screen is black, and then I want to light the screen and unlock it ), it will still send the request, and there is noSo-called wake-upSo this is not clear ......
Address: http://www.cnblogs.com/kross/p/3643708.html
Sina Weibo: http://weibo.com/KrossFord