Research on Android service notification activity update Interface

Source: Internet
Author: User

The most important component services and activities of Android are the communication problems between them. Of course, today we will not study the underlying implementation issues. I will study how to use the upper-layer applications.

First, the activity calls the service

This is relatively basic, and there are two common methods;

First, using intent, this is relatively simple. You can specify the package name and class name to call it. The intent. setclassname member can be used. Use putstring to load data. startservice (intent) is an example:

Intent regintent = new intent ("com. Service ");

Regintent. putextra ("data", "hellodata ");

Startservice (regintent );

Second, through IPC, this is troublesome and generally not needed.

But, in turn, how does the service tell the activity some states? There are two methods

First, the Service sends broadcast through broadcast. we can write a broadcastreceiver. Normally, broadcastreceiver is written as an internal class of the activity. This oncaster can directly call the activity method to update the interface. However, internal classes can only use the registerreceiver () method for code registration. static declarations cannot be made in the androidmanifest. xml file, because internal classes depend on external classes. If you must use androidmanifest to register a receiver, you can only write broadcastreceiver as a public class for a separate file. At this time, it is troublesome to update the interface. You can only run the activity you want to update on your own, then, send a broadcast message to the internal class of the activity to update the interface (the following article describes how to use broadcast)

Second, the Service sends intent directly to the activity. Startactivity in the service is a startactivity outside the activity, that is, the activity is started outside the task. Therefore, you must add a parameter in the intent as follows:

Intent intentsend = new intent (constants. action_status );

Intent1.addflags (intent. flag_activity_new_task );

Intent1.putextra ("statues", "end");

Context. startactivity (intent1 );

However, one problem that occurs at this time is that multiple startactivity attempts will cause many activity implementations to run. This is definitely not what we need. I only need one activity. At this time, set the launchmode of the activity to singleinstance in androidmanifest.

<Activity Android: Name ="Com. Demo. Activity"

Android: Label ="@ String/online"Android: launchmode ="Singleinstance">

Remember, someone can set it to singletask, but they have a difference.

Remember to update the intent so that getinstent can get the new instance each time.

@ Override

Protected VoidOnnewintent (intent ){

Setintent (intent); // write the UI update details in this method

}

 

From http://www.cnblogs.com/chenyingzhong/archive/2011/03/07/1973234.html

I also liked Daniel and helped me solve the problem !!

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.