Android Service Services--Initial knowledge

Source: Internet
Author: User

I'm sorry (to me, but also to everyone) after two weeks agreed to send the blog slow movement, because recently in with the higher mathematics dead Knock, at the same time participated in a party training, so now the study life is a little tense, their drafts have piled up a few, now I for my friend's feelings need to complete a Android the player, for some of the simple points, write some of their own sentiment.

  The first thing I want to mention when Android service services, in fact I touch the concept of service is not long, first in doing this can not be counted as engineering things, I think of, Android need to have such a mechanism, through a program to control the song playback, but also the lyrics of the display, but also the volume of the song control and so on, these need to be done in the same interface, that is, I need a "service" to silently help me control the control and display of these programs, But it's not going to affect my layout. Such a look is indeed a bit like the idea of multithreading in Java, and indeed this is based on the Android multi-threaded to achieve.

1. What do we want to know about "service"?

We can simply think of it as a silent laborer in the background. When the program is switched to the background, or if the user opens another application, the service remains operational.

There is a premise that you need to have a simple understanding of threads. Here I simply enumerate the threads of the methods used in Android :

Define a thread that only needs to create a new class to inherit from thread, and then override the parent class's run () method, and inside

Write time-consuming logic. Like what:

class extends Thread {@Overridepublicvoid  run () {//  processing specific logic }} 

Created thread we need to start it when needed, so add it after the above program:

New MyThread (). Start ();

But usually in the Android program with anonymous internal classes to use, so the above program is changed to:

New Thread (new  Runnable () {@Overridepublicvoid  run () {//  Handle the specific logic }). Start ();

2. Basic usage of "service"

 Public class extends Service {@Override public ibinder onbind (Intent Intent) {returnnull  ;}}

Here we declare a MyService class to inherit the Service and see that there is only one abstract method onbind() that needs to be rewritten. But usually we need to rewrite onCreate (),onstartcommand () , and OnDestroy () in the project that uses the service these several methods. The following is the method declaration for the above:

 Public classMyServiceextendsService {@Override Publicibinder onbind (Intent Intent) {return NULL;} @Override Public voidonCreate () {Super. OnCreate ();} @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {return Super. Onstartcommand (Intent, flags, Startid);} @Override Public voidOnDestroy () {Super. OnDestroy ();}}

The rest is only required to be registered in the androidmanifest.xml file.

We can see that we have started the service, but we cannot control when the service is entered and output, so we need to consider the activity and Service Connect and add some control at the same time. (In the following program I have added two buttons to the layout file), and the status information of the service is displayed by printing. At the same time we need to bind the service with the activity while the program is running, and then unbind it after running. At this point we need to be more detailed on the basis of the above procedures, through these methods to complete.

We also write a MyService service to inherit the services, and then rewrite the corresponding method, but this time we need to write a Binder class object, through binder to complete the activity and service binding.

 Public classMyServiceextendsService {PrivateDownloadbinder Mbinder =NewDownloadbinder ();classDownloadbinderextendsBinder { Public voidstartdownload () {LOG.D ("MyService", "Startdownload executed");} Public intgetprogress () {LOG.D ("MyService", "Getprogress executed");return0;}} @Override Publicibinder onbind (Intent Intent) {returnMbinder;}} Here's how to invoke binder on the basis of binding activities and services.  Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton StartService;PrivateButton StopService;PrivateButton Bindservice;PrivateButton Unbindservice;PrivateMyservice.downloadbinder Downloadbinder;PrivateServiceconnection connection =Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {} @Override Public voidonserviceconnected (componentname name, IBinder service) {Downloadbinder=(Myservice.downloadbinder) service;downloadbinder.startdownload ();d ownloadbinder.getprogress ();}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Bindservice=(Button) Findviewbyid (r.id.bind_service); Unbindservice=(Button) Findviewbyid (R.id.unbind_service); Bindservice.setonclicklistener ( This); Unbindservice.setonclicklistener ( This);} @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.bind_service:intent bindintent=NewIntent ( This, MyService.class); Bindservice (bindintent, Connection, bind_auto_create); //Binding Service Break; CaseR.id.unbind_service:unbindservice (connection);//Unbind Service Break;default: Break;}}}

The same service as the activity has its own life cycle, which is not elaborated here.

One small step in the day, a big step in the Month ~ Come on!!

Android Service Services--Initial knowledge

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.