Android Step-Pit Diary 1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.