Talk about the Android service

Source: Internet
Author: User

Service is one of the four components of Android, and unlike activity, it cannot interact with the user. It is a long life cycle, no visual interface, running in the background of a service program.
the service in 1.Android has no user interface, it runs in the system is not easy to be detected by the user, you can use it to develop programs such as monitoring. The development of the service is relatively simple, as follows:
First step: Inheriting the service class
Public class MyService extends Service {}
Step Two: Configure the service in the <application> node in the Androidmanifest.xml file:
<service android:name= ". MyService "/>
The service cannot run itself and needs to start the service by calling the Context.startservice () or Context.bindservice () method. Both of these methods can start the service
2.Service As with activity, there is a process from startup to destruction, but the service process is much simpler than activity. Service initiation to destruction will only go through the following 3 stages:
Create service public void onCreate ();

Start service public void OnStart (Intent Intent, int startid);

Destroy service public void OnDestroy ();

1 Myservice.java2   3   4  PackageCom.example.service; 5   6 ImportAndroid.app.Service; 7 Importandroid.content.Intent; 8 ImportAndroid.os.IBinder; 9   Ten  Public classMyServiceextendsService { One    A @Override -      Publicibinder onbind (Intent Intent) { -         //TODO auto-generated Method Stub the         return NULL;  -            -     }   -    + @Override -      Public voidonCreate () { +         //TODO auto-generated Method Stub ASystem.out.println ("# # #onCreate # # #");  at     }   -    - @Override -      Public voidOnDestroy () { -         //TODO auto-generated Method Stub -System.out.println ("# # #onDestroy # # #");  in     }   -    to @Override +      Public intOnstartcommand (Intent Intent,intFlagsintStartid) {   -         //TODO auto-generated Method Stub theSystem.out.println ("# # #onStartCommand # # #");  *         returnService.start_continuation_mask;//Continue execution $     }  Panax Notoginseng    -}

Mainactivity.java PackageCom.example.service; ImportAndroid.os.Bundle; Importandroid.app.Activity; ImportAndroid.content.DialogInterface; Importandroid.content.Intent; ImportAndroid.view.Menu; ImportAndroid.view.View; ImportAndroid.view.View.OnClickListener; ImportAndroid.widget.Button;  Public classMainactivityextendsActivity {PrivateButton start=NULL; PrivateButton stop=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.activity_main); Start=(Button) Findviewbyid (R.id.start); Stop=(Button) Findviewbyid (r.id.stop);  This. Start.setonclicklistener (NewStartonclicklistenerimpl ());  This. Stop.setonclicklistener (NewStartonclicklistenerimpl ()); }      Private  classStartonclicklistenerimplImplementsonclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method StubMainactivity. This. StartService (NewIntent (mainactivity. This, MyService.class)); }            Private  classStoptonclicklistenerimplImplementsonclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method StubMainactivity. This. StartService (NewIntent (mainactivity. This, MyService.class)); }                        }                }    } 

Take care not to forget androidmanifest.xml.

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.