First set up a intent.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <ButtonAndroid:id= "@+id/btnstartnormalservice"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start normalservice" /> <ButtonAndroid:id= "@+id/btnstartintentservice"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start intentservice" /></LinearLayout>
Establish a Myservice.java implementation thread multithreading
PackageCom.szy.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public classMyServiceextendsservice{protected Static FinalString TAG = "Intentactivity"; @Override Public voidonCreate () {Super. OnCreate (); } @Override Public voidOnDestroy () {Super. OnDestroy (); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) { NewMyThread (). Start (); returnStart_sticky; } @Override Publicibinder onbind (Intent Intent) {return NULL; } Private classMyThreadextendsThread {@Override Public voidrun () {Try{log.i (TAG,"MyService thread ID:" +Thread.CurrentThread (). GetId ()); LOG.I (TAG,"File download ...."); Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } } }}
and build a Exampleintentservice.java intentservice.
PackageCom.szy.service;ImportAndroid.app.IntentService;Importandroid.content.Intent;ImportAndroid.util.Log; Public classExampleintentserviceextendsintentservice{protected Static FinalString TAG = "Intentactivity"; PublicExampleintentservice () {Super("Exampleintentservice"); } @Overrideprotected voidonhandleintent (Intent Intent) {Try{log.i (TAG,"MyService thread ID:" +Thread.CurrentThread (). GetId ()); LOG.I (TAG,"File download ...."); Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } }}
Finally, there's an activity to build a intentactivity.xml.
Package Com.szy.service;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Intentactivity extends activity{protected static final String TAG = "intentactivity"; Private Button Btnstartnormalservice; Private Button Btnstartintentservice; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.intent); Btnstartnormalservice = (Button) Findviewbyid (R.id.btnstartnormalservice); Btnstartintentservice = (Button) Findviewbyid (R.id.btnstartintentservice); Btnstartnormalservice.setonclicklistener (listener); Btnstartintentservice.setonclicklistener (listener); } Private Onclicklistener Listener=new Onclicklistener () {public void OnClick (View v) {Intent IntenT Switch (V.getid ()) {Case R.id.btnstartnormalservice:intent=new Intent (Intentactivit Y.this, Myservice.class); LOG.I (TAG, "Main thread ID:" +thread.currentthread (). GetId ()); StartService (Intent); Break Case R.id.btnstartintentservice:intent=new Intent (intentactivity.this, exampleintentservice.class); LOG.I (TAG, "Main thread ID:" +thread.currentthread (). GetId ()); StartService (Intent); Break Default:break; } } };}
Remember to modify ANDROIDMANIFEST.MXL
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.szy.service"Android:versioncode= "1"Android:versionname= "1.0"> <USES-SDKandroid:minsdkversion= "8" /> <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"> </Activity> <ActivityAndroid:name=". Binderactivity "Android:label= "@string/app_name"> </Activity> <ActivityAndroid:name=". Intentactivity "Android:label= "@string/app_name"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ServiceAndroid:name=". Exampleservice " /> <ServiceAndroid:name=". Binderservice " /> <ServiceAndroid:name=". MyService "/> <ServiceAndroid:name=". Exampleintentservice "/> </Application></Manifest>
Android-----> Multi-Threading Implementation thread, Intentservice application