In this interview, I believe it is one of the interviewer's favorite questions. Simply put, Intentservice inherits to Service,service and Acitivity as if it were dependent on the application's main process, which is not itself a process or a thread. Some time-consuming operations may cause the ANR bug, (during this test, the service executes 20 seconds without reporting ANR), and Intentservice, looking at its source code, OnCreate () is actually creating a new thread.
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.app;import android.content.intent;import Android.os.handler;import android.os.HandlerThread; Import Android.os.ibinder;import Android.os.looper;import Android.os.message;public abstract class Intentservice Extends Service {private volatile Looper mservicelooper; private volatile Servicehandler Mservicehandler; Private String Mname; Private Boolean mredelivery; PRivate Final class Servicehandler extends Handler {public Servicehandler (Looper Looper) {super (Looper) ; } @Override public void Handlemessage (Message msg) {onhandleintent ((Intent) msg.obj); Stopself (MSG.ARG1); }} public Intentservice (String name) {super (); Mname = name; The public void Setintentredelivery (Boolean enabled) {mredelivery = enabled; } @Override public void OnCreate () {super.oncreate (); Handlerthread thread = new Handlerthread ("intentservice[" + Mname + "]"); Thread.Start (); Mservicelooper = Thread.getlooper (); Mservicehandler = new Servicehandler (mservicelooper); } @Override public void OnStart (Intent Intent, int startid) {Message msg = Mservicehandler.obtainmessage (); MSG.ARG1 = Startid; Msg.obj = Intent; Mservicehandler.sendmessage (msg); } @Override public int onstartcommand (intENT intent, int flags, int startid) {OnStart (intent, Startid); Return mredelivery? Start_redeliver_intent:start_not_sticky; } @Override public void OnDestroy () {mservicelooper.quit (); } @Override Public IBinder onbind (Intent Intent) {return null; } protected abstract void Onhandleintent (Intent Intent);}
The Looper object fetches the Intent object from MessageQueue each time, and then handler processes the message, calls void Onhandleintent (Intent Intent), where it can perform some time-consuming operations such as downloading files. Each call to Intentservice generates a new Handlerthread, so the UI main thread is not blocked. If you need to perform a time-consuming operation in a service, then Intentservice is a good choice, without the hassle of new Thread () or new Asynctask () in the service. Let's look at the demo of our experiment and compare the effects of service and Intentservice on acitivity.
First step: Create service and Intentservice
Package Com.figo.study.service;import Android.app.service;import Android.content.intent;import android.os.IBinder; public class Testservice extends Service {@Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stu Breturn null;} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate ();} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {try {thread.sleep (20000); System.out.print ("Service Execution Complete");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Return Super.onstartcommand (Intent, flags, Startid);}}
/** * */package com.figo.study.service;import android.app.intentservice;import android.content.intent;/** * @ Author Administrator * */public class Testintentservice extends Intentservice {//public testintentservice (String name) {/ TODO Auto-generated Constructor Stub//super (name);//}public Testintentservice () {Super ("Testintentservice");} /* (non-javadoc) * @see android.app.intentservice#onhandleintent (android.content.Intent) */@Overrideprotected void Onhandleintent (Intent Intent) {//TODO auto-generated method stubtry {thread.sleep (20000); System.out.print ("Intentservice execution Complete");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Step Two: Androidmanifest.xml registering service and activity
<service android:name= "Com.figo.study.service.TestService" android:exported= "false" > <intent-filter> <action android:name= "Testservice"/> </intent-filter& Gt </service> <service android:name= "Com.figo.study.service.TestIntentService" Android: Exported= "false" > <intent-filter> <action android:name= "Testintentservice"/> </intent-filter> </service> <activity android:name= "com.figo.study.Intent Serviceactivity "Android:theme=" @android: Style/theme.notitlebar "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.intent . category. LAUNCHER "/> </intent-filter> </activity>
Step Three: Create a test activity
/** * */package Com.figo.study;import Com.figo.study.service.testintentservice;import Com.figo.study.service.testservice;import Android.app.activity;import Android.app.intentservice;import Android.content.intent;import android.os.bundle;/** * @author Figo * */public class Intentserviceactivity extends Activi ty {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); try { Setcontentview (R.layout.activity_intentservice);//Start a service with a 20-second timeout, because it is attached to the main thread, and you can see that it must wait until service execution is complete before the page is displayed// Intent service=new Intent ("Testservice");//intent service = new Intent (intentserviceactivity.this,// Testservice.class);//startservice (service);//Start a 20-second time-out intentservice, because it is a new thread, so the page is displayed immediately//Intent Intentservice =new Intent ("Testintentservice"); Intent intentservice=new Intent (intentserviceactivity.this,testintentservice.class); StartService (Intentservice);} catch (Exception e) {e.printstacktrace ();}}}
The difference between 60:intentservice and service in Android development