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.