Android Four components-activity, service, broadcast, fragmentation
Situation One
Scenario: Periodically fetch data from the server and then update the UI based on the latest available data in the activity or fragmentation.
Thinking: The first time, think of the timer, recommended to use the system comes with the alertmanager, and it needs to start a pendingintent, so think of it in a service, and then start the service on a regular basis, second, to obtain data from the server, not in the main thread, So if you need to load asynchronously, think of Asyntask, and then consider that the activity or fragment is updating the UI based on the latest data, how do I update it? Think of the broadcast here, the front through the timer fixed time to start the service, then when the service is started, we can send a broadcast, recommend local broadcast, and then create a new class to listen to the broadcast, when and this class needs to implement an interface, when the monitor hears the broadcast, call this method, and finally in the activity or fragment, Start the service, bind the broadcast, and implement the method of the front interface.
Summary: Service, Broadcastreceiver, Alertmanager, Ilistener
Perfect: After running there will be two problems, one is because Alertmanager is a system, so will appear, when we release the broadcast binding, and stop the service, in a fixed time, Alertmanager again start the service, in order to solve this problem, you can create a new singleton class, Alert is used to manage the Pendingintent instances and Alertmanager instances required by Alertmanager, and then to stop the scheduled tasks at the point where the service is stopped, using the Alertmanager cancel method.
Android Step-Pit Diary 1