Asynchronous intentservice of services in Android (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.