Reprint Address: http://www.cnblogs.com/zhangs1986/p/3602154.html
Intentservice: Asynchronous Processing Service, opening a new thread : Handlerthread a message in the thread, and then accepts that processing completes, cleans up the threads, and shuts down the service.
Intentservice has the following features:
(1) It creates a separate worker thread to handle all the intents that pass through Onstartcommand () to the service.
(2) Create a task queue to send intent to onhandleintent ().
(3) Do not need to call STOPSELFT () actively to end the service. Because, after all intent are processed, the system automatically shuts down the service.
(4) The default implementation of Onbind () returns null
(5) The purpose of the default implementation of Onstartcommand () is to insert intent into the work queue
Classes that inherit Intentservice must implement at least two functions: constructors and Onhandleintent () functions . To overwrite other functions of intentservice, be careful to call the corresponding function of the parent class through Super.
Interface settings Two button:
<button android:id= "@+id/btnstartintentservice" android:layout_width= "Match_parent" android: layout_height= "Wrap_content" android:text= "start Download" > </Button> <button android:id= "@+ Id/btnstopintentservice " android:layout_width=" match_parent " android:layout_height=" Wrap_content " android:text= "End Download" > </Button>
Declaring Intentservicesub inheritance Intentservice
public class Intentservicesub extends Intentservice { private static final String TAG = "Intentservicesub"; Public Intentservicesub () { super ("Intentservicesub"); LOG.I (TAG, "=>intentservicesub"); } /* (Non-javadoc) * @see android.app.intentservice#oncreate () */ @Override public void OnCreate () { log.i (TAG, "=>oncreate"); Super.oncreate (); } /* (Non-javadoc) * @see Android.app.intentservice#ondestroy () */ @Override public void OnDestroy () { log.i (TAG, "=>ondestroy"); Super.ondestroy (); } @Override protected void onhandleintent (Intent arg0) { log.i (TAG, "Intentservice Thread:" + Thread.currentThread.getId ());
}
Page button Events
Btnstartintentservice = (Button) This.findviewbyid (r.id.btnstartintentservice); Btnstopintentservice = (Button) This.findviewbyid (r.id.btnstopintentservice);p rivate onclicklistener listener = new Onclicklistener () { @Override public void OnClick (View v) {case R.id.btnstartintentservice: log.i ( TAG, "Main thread ID:" +thread.currentthread.getid ()); if (mserviceintent = = null) mserviceintent = new Intent (androidserviceactivity.this,intentservicesub.class); StartService (mserviceintent); break; Case R.id.btnstopintentservice: log.i (TAG, "Btnstopintentservice"); if (mserviceintent! = null) { stopservice (mserviceintent); Mserviceintent = null; } Break;}} ;
Asynchronous intentservice of services in Android (ii)