1.IntentService
(1).介紹Service
Android中的Service是用于后臺服務(wù)的,當(dāng)應(yīng)用程序被掛起到后臺或者啟動Service的Activity被銷毀時,為了保證應(yīng)用某些組件仍然可以工作而引入了Service這個概念,但是Service既不是獨立的進(jìn)程,也不是獨立的線程,它是依賴于應(yīng)用程序的主線程的,在大部分時候不建議在Service中編寫耗時的邏輯和操作,否則會引起ANR。(阻塞主線程5s,會導(dǎo)致ANR)
(2).IntentService||JobIntentService應(yīng)用場景
如果我們編寫的耗時邏輯,不得不被service來管理的時候,就需要使用IntentService,IntentService是繼承Service的,那么它包含了Service的所有特性,當(dāng)然也包含service的生命周期,那么與service不同的是,IntentService在執(zhí)行onCreate操作的時候,內(nèi)部開了一個線程,去你執(zhí)行你的耗時操作。
public abstract class IntentService extends Service {
protected abstract void onHandleIntent(@Nullable Intent intent);
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 void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
}
(3).繼承抽象類IntentService,然后實現(xiàn)handleIntent方法即可.
public class TestService extends IntentService {
public TestService(String name) {
super(name);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
}
}
(4).HandleThread實現(xiàn)
繼承了Thread,Looper.prepare()將當(dāng)前線程和Looper對象存儲到ThreadLocalMap中,然后提供getLooper方法。
public class HandlerThread extends Thread {
public void run(){
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
}
public Looper getLooper() {
return mLooper;
}
}
2.JobIntentService
(1).通過內(nèi)部的AysncTask來處理耗時操作
public abstract class JobIntentService extends Service {
protected abstract void onHandleWork(@NonNull Intent intent);
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
ensureProcessorRunningLocked(true);
}
void ensureProcessorRunningLocked(boolean reportStarted) {
if (mCurProcessor == null) {
mCurProcessor = new CommandProcessor();
if (mCompatWorkEnqueuer != null && reportStarted) {
mCompatWorkEnqueuer.serviceProcessingStarted();
}
mCurProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
final class CommandProcessor extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
GenericWorkItem work;
if (DEBUG) Log.d(TAG, "Starting to dequeue work...");
while ((work = dequeueWork()) != null) {
if (DEBUG) Log.d(TAG, "Processing next work: " + work);
onHandleWork(work.getIntent());//回調(diào)onHandlerWork方法
if (DEBUG) Log.d(TAG, "Completing work: " + work);
work.complete();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
processorFinished();
}
}
}
(2).使用繼承抽象類JobIntentService,實現(xiàn)onHandleWork來處理耗時操作
public class TestService extends JobIntentService {
@Override
protected void onHandleWork(@NonNull Intent intent) {
//在AsyncTask啟動的異步線程中執(zhí)行
}
}