Research on Service in android Development
I learned about Service technology when I was studying android development in two years.
I haven't taken the time to sort this content. Now I have time to get a detailed and in-depth understanding of this Service.
I. Service and Activity
Android development is inseparable from Activity. The Activity is equivalent to the window of WIN.
However, there is usually only one reality. In the front-end, other activities are either pushed to the back-end stack or manually destroyed.
The Service can start multiple services, so multiple services are in parallel state.
And the Service can be relatively independent from the Activity.
In other words, you can start the Service in ActivityA.
Send a message to the Service in ActivityB
Disable the Service in ActivityC.
These activities take turns at the front end.
Ii. Service and thread
In fact, it is easy to understand the Service if you understand the thread.
The Activity Service is executed in the main thread, but only one Activity object is displayed on the front end.
The Service is not displayed on the front end and multiple objects can be generated.
In this way, the data stored in the Service will not be destroyed as the data in the Activity is destroyed.
In addition, a new thread can be enabled as the background processing thread.
It is completely independent from the Activity that only processes messages sent from the Activity.
Iii. Code
In manifest
Register two activities and one Service
Layout
Activity_main: Give the first activity three buttons to start the Service and end the Service and the next interface.
Activity_sec The second activity also sends messages to the three buttons to exit the interface. 2. End the Service.
MainActivity button Processing
Public void onClick (View arg0) {// TODO Auto-generated method stub switch (arg0.getId () {case (R. id. button_on): // start Service this. startService (new Intent (this, PlayService. class); break; case (R. id. button_off): {// stop Service this. stopService (new Intent (this, PlayService. class);} break; case (R. id. button_next): {// open another Activity Intent intent = new Intent (); intent. setClass (MainActivity. this, SecActivity. class); startActivity (intent);} break; default: break ;}}
SecActivity button Processing
Public void onClick (View arg0) {switch (arg0.getId () {case (R. id. button_esc): // exit Activity finish (); break; case (R. id. button_off): {// disable Service this. stopService (new Intent (this, PlayService. class);} break; case (R. id. button_step2): {// send Intent intent = new Intent (this, PlayService. class); Bundle bundle = new Bundle (); bundle. putInt ("op", 2); intent. putExtras (bundle); this. startService (intent);} break; default: break ;}}
Complete PlayService code
Public class PlayService extends Service {private HandlerThread mHandlerThread; private Handler mPlayHandler; class PlayHandler extends Handler {public PlayHandler () {} public PlayHandler (low.lp) {super (lp );} @ Override public void handleMessage (Message msg) {// TODO Auto-generated method stub super. handleMessage (msg); // receives the data processed by the thread. // note that the System is not on the main thread but on an independent thread. out. println ("PlayHandler handle Message "); System. out. println ("PlayHandler thread id:" + Thread. currentThread (). getId () ;}}@ Override public IBinder onBind (Intent intent) {// TODO Auto-generated method stub System. out. println ("PlayService on onBind"); return null ;}@ Override public void onCreate () {// TODO Auto-generated method stub super. onCreate (); Toast. makeText (this, "Play Service Created", Toast. LENGTH_SHORT ). show (); Sys Tem. out. println ("PlayService on onCreate"); // start the thread mHandlerThread = new HandlerThread ("Play_Service_thread"); mHandlerThread. start (); mPlayHandler = new PlayHandler (mHandlerThread. getlodid () ;}@ Override public void onStart (Intent intent, int startId) {// TODO Auto-generated method stub super. onStart (intent, startId); // onStart can be used to receive messages sent from the Activity. // note that it is still in Toast on the main thread. makeText (this, "Play Servi Ce onStart ", Toast. LENGTH_SHORT ). show (); System. out. println ("PlayService on onStart"); System. out. println ("PlayService thread id:" + Thread. currentThread (). getId (); if (intent! = Null) {Bundle bundle = intent. getExtras (); if (bundle! = Null) {int op = bundle. getInt ("op"); switch (op) {case 2: // forward the Message object to the thread for processing System. out. println ("PlayService on onStart step2"); Message msg = mPlayHandler. obtainMessage (); msg. sendToTarget (); break ;}}// if} @ Override public void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); Toast. makeText (this, "Play Service Stopped", Toast. LENGTH_SHORT ). show (); System. out. println ("PlayService on Destroy"); // exit the thread to Destroy mHandlerThread. quit (); mHandlerThread = null; mPlayHandler = null ;}}
Service generation and destruction can be called within the Activity
But the Service life cycle is not equal to Activity, but in the main thread.
Zookeeper