This article mainly introduces the Android service (background services) in detail, this article explains the service concept, role, lifecycle, startup mode and code examples, etc., the need for friends can refer to the
1. Concept:
(1). Service can be said to be a running activity in the background. It's not a separate process, it just needs to be applied to tell it what to do in the background.
(2). If it implements and interacts with the user, it needs to receive the display through the notification bar or by sending a broadcast.
(3). Its application is very extensive, especially in the frame layer, the application is more to the system service calls.
2. Function:
(1). It is used to handle some background operations that do not interfere with the user's use. The following download, network access. Play music, which can be turned on by intent, and can also be bound to host objects (callers, for example, on activity) for use.
(2). If the activity is to display the information of the front page, then the service is operated in the background. If the service interacts with the foreground UI, it can be done by sending a broadcast or notification bar.
3. Life cycle:
(1). The life time of the service as a whole is from oncreate () to the start of the call, until the OnDestroy () method returns. As with activity, the service carries out its initialization in OnCreate (), releasing the remaining resources in the OnDestroy ().
(2). **startservice () Way: **oncreate ()->onstartcommand ()->onstart ()->ondestroy ()
(3). **bindservice (): **oncreate ()->onbinder ()->onunbind ()->ondestroy (). The Onunbind () method ends when it returns.
4. Starting mode:
(1). Service cannot run itself, it needs to be invoked through an activity or other context object.
(2). There are two ways to start a service:
Context.startservice () and Context.bindservice () Start the service in two ways. If there is a time-consuming operation in the service's OnCreate () method or the OnStart () method, a new thread is opened. Start in both of these ways where you need a service.
Attention:
Usually use a lot of StartService method, you can put some time-consuming tasks into the background to deal with, when the processing is completed, you can broadcast or notify the bar to inform the front desk.
5. The following code to further understand:
(1). Mainactivity.java class:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45, 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.example.servicetest; Import Com.example.servicetest.service.MyService; Import android.app.Activity; Import Android.content.ComponentName; Import android.content.Intent; Import android.content.ServiceConnection; Import Android.os.Bundle; Import Android.os.IBinder; Import Android.util.Log; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; public class Mainactivity extends activity implements Onclicklistener {/** flag bit */private static String TAG = "com . example.servicetest.MainActivity "; /** Start Service * * Private Button Mbtnstart; /** Binding Service * * Private Button mbtnbind; @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } /** * init the View */private void Initview () {Mbtnstart = (Button) Findviewbyid (r.id.startservice); mbtnbind = (Button) Findviewbyid (r.id.bindservice); Mbtnstart.setonclicklistener (this); Mbtnbind.setonclicklistener (this); } @Override public void OnClick (view view) {switch (View.getid ()) {//Start service way Case R.id.startservice:star TService (New Intent (myservice.action)); Break The way of binding services case R.id.bindservice:bindservice (new Intent (myservice.action), Conn, bind_auto_create); break; Default: break; } } serviceconnection conn = new Serviceconnection () {public void onserviceconnected (componentname name, IB Inder service) {LOG.V (TAG, "onserviceconnected");} public void onservicedisconnected (componentname name) {LOG.V ( TAG, "onservicedisconnected"); } }; @Override protected void OnDestroy () {Super.ondestroy (); System.out.println ("-------OnDestroy ()-"); StopService (New Intent (myservice.action)); Unbindservice (conn); } } |
(2). Myservice.java class:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the same as the same as the other, at the same-------------------------- |
package com.example.servicetest.service; Import Android.app.Service; Import android.content.Intent; Import Android.os.Binder; Import Android.os.IBinder; Import Android.util.Log; public class MyService extends Service {/** logo bit/private static String TAG = "Com.example.servicetest.service.My Service "; /** Behavior * * public static final String action = "Com.example.servicetest.service.MyService"; @Override public void OnCreate () {super.oncreate () SYSTEM.OUT.PRINTLN ("-----onCreate ()---"); } &N Bsp @Override public void OnStart (Intent Intent, int startid) {Super.onstart (Intent, Startid); SYSTEM.OUT.PRINTLN ("-----onStart ()---"); } @Override public int Onstartcommand (inteNT Intent, int flags, int startid) {System.out.println ("-----onstartcommand ()---"); return Super.onstartcommand (Intent , flags, Startid); } @Override public ibinder onbind (Intent arg0) {System.out.println ("-----onbind ()---"); return null;} &N Bsp @Override public void Onrebind (Intent Intent) {System.out.println ("-----onrebind ()---"); Super.onrebind (Intent);} &NB Sp @Override public boolean onunbind (Intent Intent) {System.out.println ("-----onunbind ()---"); return Super.onunbind ( Intent); } @Override public void OnDestroy () {System.out.println ("-----ondestroy ()---"); Super.ondestroy ();} } |
(3). Androidmanifest.xml
?
1 2 3 4 5 6 7 8 9 10 |
<!--registered--> <service android:name= "Com.example.servicetest.service.MyService" > <intent-filter> <!--intent--> <action android:name= "Com.example.servicetest.service.MyService"/> To start the service Android:name= "Android.intent.category.default"/> </intent-filter> </service> |
StartService Method:
(1). When the StartService button is pressed:
(2). When you continue to press the StartService button:
Bindservice Method:
(1). When the Bindservice button is pressed:
(2). When you continue to press the Bindservice button:
(3). Press the StartService button and press the Bindservice button first: