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,Write broadcastreceiver as the internal class of the activity.The oncycler can directly call the activity method to update the interface. However, internal classes can only useCodeRegistration Method
Registerreceiver ()You cannot make static declarations 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, and then send a broadcast message to the internal class of the activity to update the interface.

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 the following parameter to the intent::

Intent intentsend = new intent (constants. action_status );

Intent1.addflags (intent. flag_activity_new_task );

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

Context. startactivity (intent1 );

However, a 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, we need to face this activity in androidmanifestSet launchmode to singleinstance

<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 );

}

Services running in the background sometimes need to notify the foreground activity to send some data to the activity. For exampleProgramCommunication between the service and the activity using intent is a good solution. Register a broadcastreceiver in the activity. When the Service sends data to the activity, construct an intent and call the sendbroadcast () method to send the data to the activity.

modify the content based on chapter8_1 to enable mainactivity to receive notifications from musicservice after the music is played. First, register an oncompletionlistener for the mediaplayer. When the playing ends, send intent to the mainactivity.

  
  
  1. mediaplayer. oncompletionlistener listener = new mediaplayer. oncompletionlistener () {
  2. Public void oncompletion (mediaplayer arg0) {
  3. // musicservice sends data to mainactivity in broadcast mode
  4. intent = new intent (music_completed);
  5. intent. putextra ("MSG", gettext (R. String. music_completed);
  6. sendbroadcast (intent);
  7. }
  8. };

Next, register a broadcastreceiver for mainactivity to listen to information from musicservice. When the musicreceiver receives a broadcast from the musicservice, a toast prompt is displayed, prompting the user. The code added to mainactivity is as follows:

 
 
  1. Class musicreceiver extends broadcastreceiver {
  2. @ Override
  3. Public void onreceive (context arg0, intent arg1 ){
  4. String action = arg1.getaction ();
  5. If (action. Equals (musicservice. music_completed )){
  6. // Receives broadcast messages from musicservice. The toast is displayed.
  7. String MSG = arg1.getstringextra ("MSG ");
  8. Toast. maketext (mainactivity. This, MSG, Toast. length_short)
  9. . Show ();
  10. }
  11. }
  12. }
  13. @ Override
  14. Protected void onpause (){
  15. // Cancel broadcastreceiver
  16. Unregisterreceiver (receiver );
  17. Super. onpause ();
  18. }
  19.  
  20. @ Override
  21. Protected void onresume (){
  22. Intentfilter filter = new intentfilter (musicservice. music_completed );
  23. If (else ER = NULL)
  24. Receiver = new musicreceiver ();
  25. // Register broadcastreceiver
  26. Registerreceiver (receiver, filter );
  27. Super. onresume ();
  28. }

If multiple activities need to register broadcastreceiver to listen to messages from the service, what should I do? Registering One by one is obviously troublesome. You can define a parent activity to register broadcastreceiver. Other activities inherit this parent class to simplify the program.

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.