Android面试题Service,Android面试题-IntentService源码分析
自定義控件
聯(lián)網(wǎng)
工具
數(shù)據(jù)庫
源碼分析相關(guān)面試題
Activity相關(guān)面試題
Service相關(guān)面試題
與XMPP相關(guān)面試題
與性能優(yōu)化相關(guān)面試題
與登錄相關(guān)面試題
與開發(fā)相關(guān)面試題
與人事相關(guān)面試題
人事面試寶典
IntentService是繼承于Service并處理異步請(qǐng)求的一個(gè)類,在IntentService內(nèi)有一個(gè)工作線程來處理耗時(shí)操作,啟動(dòng)IntentService的方式和啟動(dòng)傳統(tǒng)Service一樣,同時(shí),當(dāng)任務(wù)執(zhí)行完后,IntentService會(huì)自動(dòng)停止,而不需要我們?nèi)ナ謩?dòng)控制。另外,可以啟動(dòng)IntentService多次,而每一個(gè)耗時(shí)操作會(huì)以工作隊(duì)列的方式在IntentService的onHandleIntent回調(diào)方法中執(zhí)行,并且,每次只會(huì)執(zhí)行一個(gè)工作線程,執(zhí)行完第一個(gè)再執(zhí)行第二個(gè),以此類推。
而且,所有請(qǐng)求都在一個(gè)單線程中,不會(huì)阻塞應(yīng)用程序的主線程(UI Thread),同一時(shí)間只處理一個(gè)請(qǐng)求。
IntentService有什么好處呢?
1)我們省去了在Service中手動(dòng)開線程的麻煩,
2)當(dāng)操作完成時(shí),我們不用手動(dòng)停止Service。
接下來讓我們來看看如何使用,寫一個(gè)Demo來模擬兩個(gè)耗時(shí)操作,Operation1與Operation2,先執(zhí)行1,2必須等1執(zhí)行完才能執(zhí)行2:
新建工程,新建一個(gè)繼承IntentService的類,我這里是IntentServiceDemo.java
public class IntentServiceDemo extends IntentService {
public IntentServiceDemo() {
//必須實(shí)現(xiàn)父類的構(gòu)造方法
super("IntentServiceDemo");
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return super.onBind(intent);
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(enabled);
System.out.println("setIntentRedelivery");
}
@Override
protected void onHandleIntent(Intent intent) {
//Intent是從Activity發(fā)過來的,攜帶識(shí)別參數(shù),根據(jù)參數(shù)不同執(zhí)行不同的任務(wù)
System.out.println("currentThread()=" + Thread.currentThread().getName());
String action = intent.getExtras().getString("param");
if (action.equals("oper1")) {
System.out.println("Operation1");
}else if (action.equals("oper2")) {
System.out.println("Operation2");
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
}
我把生命周期方法全打印出來了,待會(huì)我們來看看它執(zhí)行的過程是怎樣的。接下來是Activity,在Activity中來啟動(dòng)IntentService:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//可以啟動(dòng)多次,每啟動(dòng)一次,就會(huì)新建一個(gè)work thread,但I(xiàn)ntentService的實(shí)例始終只有一個(gè)
//Operation 1
Intent startServiceIntent = new Intent("com.test.intentservice");
Bundle bundle = new Bundle();
bundle.putString("param", "oper1");
startServiceIntent.putExtras(bundle);
startService(startServiceIntent);
//Operation 2
Intent startServiceIntent2 = new Intent("com.test.intentservice");
Bundle bundle2 = new Bundle();
bundle2.putString("param", "oper2");
startServiceIntent2.putExtras(bundle2);
startService(startServiceIntent2);
}
}
最后,別忘了配置Service,因?yàn)樗^承于Service,所以,它還是一個(gè)Service,一定要配置,否則是不起作用的
最后來看看執(zhí)行結(jié)果:
從結(jié)果可以看到,onCreate方法只執(zhí)行了一次,而onStartCommand和onStart方法執(zhí)行了兩次,開啟了兩個(gè)Work Thread,這就證實(shí)了之前所說的,啟動(dòng)多次,但I(xiàn)ntentService的實(shí)例只有一個(gè),這跟傳統(tǒng)的Service是一樣的。Operation1也是先于Operation2打印,并且我讓兩個(gè)操作間停頓了2s,最后是onDestroy銷毀了IntentService。
IntentService 源碼分析
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
源碼可知:
1)實(shí)際上是使用了一個(gè) HandlerThread 來維護(hù)線程的,
2) HandleThread 中,內(nèi)部已經(jīng)維護(hù)一個(gè) Looper,這里直接使用 HandlerThread 的 Looper 對(duì)象,便于在 IntentService 中去維護(hù)消息隊(duì)列,
3)創(chuàng)建的 mServiceHandler 是屬于 HandleThread 這個(gè) WorkerThread 的。
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);
}
}
源碼可知:
1)直接把消息交給 onHandleIntent() 方法去執(zhí)行具體的業(yè)務(wù)邏輯
2)執(zhí)行完成之后,立即調(diào)用 stopSelf() 方法停止自己
接下來分析start源碼
@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;
}
源碼可知
1)在 onStartCommand() 中直接調(diào)用了 onStart() 方法
2)而上面 stopSelf() 方法使用的 startId 來停止當(dāng)前的此次任務(wù)服務(wù)。
3)而 Service 如果被啟動(dòng)多次,就會(huì)存在多個(gè) startId ,當(dāng)所有的 startId 都被停止之后,才會(huì)調(diào)用 onDestory() 自我銷毀。
我們?cè)诳纯碒andlerThread啟動(dòng)之后的源碼
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
源碼可知
1)run方法里面添加了鎖,這也解釋了為什么多次 start 同一個(gè) IntentService 它會(huì)順序執(zhí)行,全部執(zhí)行完成之后,再自我銷毀。
歡迎關(guān)注微信公眾號(hào),長(zhǎng)期推薦技術(shù)文章和技術(shù)視頻
微信公眾號(hào)名稱:Android干貨程序員
總結(jié)
以上是生活随笔為你收集整理的Android面试题Service,Android面试题-IntentService源码分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android.mk 比较字变量,粉丝投
- 下一篇: android 仿京东地址选择_Andr