Services that interact with users need to process user operations and complete corresponding functions.
I. Lifecycle
The binding method is used for interaction with users, and the onBind method is triggered once. In this method, user operations can be performed. After unbinding is canceled, the unBind method is triggered, if the Service started by startService is used at this time, the onRebind method is triggered when the binding is canceled and the onDestory method is triggered when the Service is terminated.
Ii. Creation and Method
When using the Service, you must inherit from the Service abstract class to implement the methods. Common methods include:
1) onCreate: The method triggered when the Service is started to create and initialize the Service.
2) onBind: bind the Service to complete some operations
3) onUnbind: unbind
4) onRebind: rebind
5) onDestory: Method triggered when the Service is disabled
Iii. Start and Stop Methods
Startup method: bindService (intent object, current Activity object this, BIND_AUTO_CREATE automatically create Service constant)
Termination method: unbindService (current Activity object this)
4. Service implementation during interaction with users
1) create a class and inherit from the Service. Because the onBind method returns an IBinder object, we create an internal class in the class so that it can pass this IBinder object externally, how to obtain the Service
2) implement the above methods and add the timing output function
Public class MyService extends Service {private Timer timer; private TimerTask task; private int I = 0; // provides the Binder object, the method for obtaining the Service is provided externally. private MyBinder biner = new MyBinder (); // note the returned method. class MyBinder extends Binder {public MyService getMyService () {return MyService. this ;}@ Override public IBinder onBind (Intent intent) {System. out. println ("onBind -----"); startTimer (); // pay attention to the start position *****. If it is placed here, the thread timer return biner will be run only when it is bound; // remember to return binder} @ Override public int onStartCommand (Intent intent, int flags, int startId) {System. out. println ("onStartCommand ------"); return super. onStartCommand (intent, flags, startId) ;}@ Override public void onCreate () {System. out. println ("onCreate ----"); // startTimer (); if it is put here, start will be running super. onCreate () ;}@ Override public boolean onUnbind (Intent intent) {System. out. println ("onUnbind ------"); // stopTimer (); If the ReBinder is put here, it cannot be executed. It should be placed in Destory // return super. onUnbind (intent); in this case, the ReBinder return true cannot be triggered; // The ReBinder} @ Override public void onRebind (Intent intent) {System. out. println ("onRebind ------"); super. onRebind (intent) ;}@ Override public void onDestroy () {System. out. println ("onDestory ------"); stopTimer (); // call super to stop the timer method. onDestroy () ;}// start timer public void startTimer () {Timer = new timer (); task = new TimerTask () {@ Override public void run () {I ++; System. out. println (I) ;}}; timer. schedule (task, 1000,100 0);} // stop the timer public void stopTimer () {timer. cancel () ;}public int getI () {return I;} public void setI (int I) {this. I = I ;}}
|
3) add three buttons on the main interface to bind, unbind, and terminate the three functions respectively. The main class must implement the ServiceConnection interface and its methods
◆ OnServiceConnected: the method used to bind a Service. Create an IBinder object and obtain the Service object.
◆ OnServiceDisconnected: The method automatically called by the system.
Public class MainActivity extends Activity implements OnClickListener, ServiceConnection {private Button bindBtn, unbindBtn, getServiceBtn, startBtn, stopBtn; // declare the five buttons on the Interface private Intent intent; // INtent object private MyService myService; // Service object @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // find the resource bindBtn = (Button) findViewById (R. id. bind); unbindBtn = (Button) findViewById (R. id. unbind); getServiceBtn = (Button) findViewById (R. id. getService); startBtn = (Button) findViewById (R. id. start); stopBtn = (Button) findViewById (R. id. stop); // Add listening bindBtn. setOnClickListener (this); unbindBtn. setOnClickListener (this); getServiceBtn. setOnClickListener (this); startBtn. setOnClickListener (this); stopBtn. setOnClickListener (this); // Intent Object Instantiation intent = new Intent (this, MyService. class) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. bind: bindService (intent, this, BIND_AUTO_CREATE); // bind Service break; case R. id. unbind: unbindService (this); // unbind break; case R. id. getService: Toast. makeText (MainActivity. this, "the current I value is" + myService. getI (), Toast. LENGTH_SHORT ). show (); // obtain the background information break; case R. id. start: startService (intent); // start Service break; case R. id. stop: stopService (intent); // terminate Service break;} // here, write a method to unbind the Service, to ensure that the main interface exits, you must terminate Serivce @ Override protected void onDestroy () {unbindService (this); super. onDestroy () ;}// rewrite the method in ServiceConnection of the interface to achieve a connection, and obtain the Service object @ Override public void onServiceConnected (ComponentName, IBinder service) {MyService. myBinder binder = (MyBinder) service; myService = binder. getMyService () ;}// rewrite the method in ServiceConnection of the interface. The method automatically called by the system @ Override public void onServiceDisconnected (ComponentName name ){}}
|
4) Results
There are only a few cases listed here. You must know that depending on the call location of the Service method, different results will be produced. Here we only analyze the situation in the example
◆ Directly click the Bind button to trigger the onCreate and onBind methods. Because the start timer method is placed in the onBind method, the timer output is directly
◆ When you click the getService button, you can obtain the current I value.
◆ Click the onUnbind method to trigger the onUnbind and onDestory methods. Because the stop timer operation is placed in the onunbind method, the timer output is stopped at this time.
◆ If you click the Start button, you can see that only the onCreate and onStartCommand methods are triggered, and no timer operation is triggered.
◆ Click the onBind method. You can see that only the onBind method is triggered and the timer output is started.
◆ When you click the getService button, you can obtain the current I value.
◆ At this time, you can only click the onUnbind button on the stand-alone Stop button to terminate the Service because there is no termination statement in the Service. At this time, the onUnbind and onDestory methods are triggered.
There are many other cases that we have to study on our own... 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1035423434-0.gif "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1286663