天天看點

android intentService 學習

當啟動一個Service時,他預設都是運作在主線程的,如果Service将要運作非常耗時或者可能被阻塞的操作時,應用程式将會被挂起,甚至會出現ANR錯誤。為了避免這一問題,應該在Service中重新啟動一個新的線程來進行這些操作。但有一個更好的方法那就是用IntentService

IntentService使用隊列的方式将請求的Intent加入隊列,然後開啟一個工作線程來處理隊列中的Intent,對于異步的startService請求,IntentService會處理完成一個之後再處理第二個,每一個請求都會在一個單獨的worker thread中處理,不會阻塞應用程式的主線程,這裡就給我們提供了一個思路,如果有耗時的操作與其在Service裡面開啟新線程還不如使用IntentService來處理耗時操作:

service.java:

[java]

package com.morgen.service;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

public class MyService extends Service {

@Override

public void onCreate() {

super.onCreate();

}

@Override

public void onStart(Intent intent, int startId) {

super.onStart(intent, startId);

//Service裡面是不能進行耗時的操作的,必須要手動開啟一個工作線程來處理耗時操作

System.out.println("onStart");

try {

Thread.sleep(20000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("睡眠結束");

}

@Override

public IBinder onBind(Intent intent) {

return null;

}

}

Intentservice.java:

[java]

package com.morgen.service;

import android.app.IntentService;

import android.content.Intent;

public class MyIntentService extends IntentService {

public MyIntentService() {

super("m");

}

@Override

protected void onHandleIntent(Intent intent) {

// IntentService裡面是可以進行耗時的操作的

System.out.println("onStart");

try {

Thread.sleep(20000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("睡眠結束");

}

}

入口代碼:

[java]

package com.morgen.service;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

public class ServiceDemoActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

startService(new Intent(this,MyService.class));//主界面阻塞,會出現Application not responding

//連續兩次啟動IntentService,會發現應用程式不會阻塞,第二次的請求會再第一個請求結束之後運作

startService(new Intent(this,MyIntentService.class));

startService(new Intent(this,MyIntentService.class));

}

}

來看看IntenService的源碼(android 4.0):

[java]

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);

}

}

可以看出IntentService不僅有服務的功能,還有處理和循環消息的功能.

下面是onCreate()的源碼:

[java]

public void onCreate() {

super.onCreate();

HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");

thread.start();

mServiceLooper = thread.getLooper();

mServiceHandler = new ServiceHandler(mServiceLooper);

}

IntentService建立時就會建立Handler線程并且啟動,然後再得到目前線程的Looper對象來初始化IntentService的ServiceLooper,接着建立Servicehandler對象.

下面是onStart()的源碼:

[java]

public void onStart(Intent intent, int startId) {

Message msg = mServiceHandler.obtainMessage();

msg.arg1 = startId;

msg.obj = intent;

mServiceHandler.sendMessage(msg);

}

當啟動IntentService的時候,就會産生一條附帶startId和Intent的Message并發送到MessageQueue中,接下來Looper發現MessageQueue中有Message的時候,就會停止Handler處理消息,接下來處理的代碼如下:

[java]

public void handleMessage(Message msg) {

onHandleIntent((Intent)msg.obj);

stopSelf(msg.arg1);

}

}

調用onHandleIntent((Intent)msg.obj),可以在這個方法裡面處理我們的工作.當任務完成時就會調用stopSelf(msg.arg1)這個方法來結束指定的工作

當所有的工作執行完後:就會執行onDestroy方法:

[java]

public void onDestroy() {

mServiceLooper.quit();

}

從源碼中我們可以看到,IntentService是一個基于消息的服務,每次啟動該服務并不是馬上處理你的工作,而是首先會建立對應的Looper和Handler并且在MessageQueue中添加的附帶客戶Intent的Message對象,當Looper發現有Message的時候接着得到Intent對象通過在onHandleIntent((Intent)msg.obj)中調用你的處理程式.處理完後即會停止自己的服務.接着處理MessageQueue的下一個Message對象。

轉:http://www.2cto.com/kf/201302/191986.html