多线程的应用
- 继承Thread类
- 实现Runnable接口
- AsyncTask
- Handler
- HandlerThread
- IntentService
实现步骤
步骤1:定义IntentService的子类:传入线程名称、复写onHandleIntent()方法
步骤2:在Manifest.xml中注册服务
步骤3:在Activity中开启Service服务
package com.newbeeair.cleanser.services;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.joshdholtz.sentry.Sentry;
import com.kf5sdk.init.KF5SDKInitializer;
import com.newbeeair.cleanser.BuildConfig;
import com.newbeeair.cleanser.constants.AppConstants;
import com.newbeeair.cleanser.umeng.PushManager;
import com.newbeeair.cleanser.utils.DebugLog;
import com.newbeeair.cleanser.utils.PreferenceUtils;
import com.umeng.analytics.MobclickAgent;
import com.youzan.sdk.YouzanSDK;
import cn.sharesdk.framework.ShareSDK;
/**
* <pre>
* author : lzy
* e-mail : zanyang.lin@newbeeair.com
* time : 2017/07/11
* desc : 初始化IntentService
* </pre>
*/
public class InitService extends IntentService {
private static final String ACTION_INIT = "com.newbeeair.cleanser.action.init";
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public InitService() {
super("InitService");
}
public static void startInit(Context context) {
Intent intent = new Intent(context, InitService.class);
intent.setAction(ACTION_INIT);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
DebugLog.e("initService onHandleIntent");
if (intent != null) {
final String action = intent.getAction();
if (ACTION_INIT.equals(action)) {
DebugLog.e(">>>>>>>>>>>>>>>>>>>>>>>>>>");
PreferenceUtils.initialize(this);
initYiChuangYun(this);
//initGrowingIO(this);
initUmengPush(this);
initFresco(this);
initSentry(this);
initYouzan(this);
initUmeng(this);
initShareSDK(this);
}
}
}
private void initShareSDK(Context context) {
ShareSDK.initSDK(context);
}
private void initGrowingIO(Context context) {
}
private void initFresco(Context context) {
Fresco.initialize(context);
}
private void initYiChuangYun(Context context) {
KF5SDKInitializer.initialize(context);
}
private void initUmengPush(Context context) {
PushManager.getInstance(context).registPushAgent();
}
private void initYouzan(Context context) {
YouzanSDK.init(context, AppConstants.YOUZAN_USERAGENT);
}
private void initSentry(Context context) {
Sentry.init(context, BuildConfig.SENTRY_DSN);
}
private void initUmeng(Context context) {
MobclickAgent.setDebugMode(false);// 测试模式
MobclickAgent.openActivityDurationTrack(false);// 禁止默认的页面统计方式
MobclickAgent.enableEncrypt(true);// 日志加密
MobclickAgent.setScenarioType(context, MobclickAgent.EScenarioType.E_UM_NORMAL);// 场景设置
}
}
步骤2:在Manifest.xml中注册服务
<service android:name=".services.InitService" />
区别
Service
Service 是长期运行在后台的应用程序组件。
Service 不是一个单独的进程,它和应用程序在同一个进程中,Service 也不是一个线程,它和线程没有任何关系,所以它不能直接处理耗时操作。如果直接把耗时操作放在 Service 的 onStartCommand() 中,很容易引起 ANR .如果有耗时操作就必须开启一个单独的线程来处理
IntentService
IntentService 是继承于 Service 并处理异步请求的一个类,在 IntentService 内有一个工作线程来处理耗时操作,启动 IntentService 的方式和启动传统 Service 一样,同时,当任务执行完后,IntentService 会自动停止,而不需要我们去手动控制。另外,可以启动 IntentService 多次,而每一个耗时操作会以工作队列的方式在IntentService 的 onHandleIntent 回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
而且,所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。 那么,用 IntentService 有什么好处呢?首先,我们省去了在 Service 中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止 Service
源码分析
// IntentService源码中的 onCreate() 方法
@Override
public void onCreate() {
super.onCreate();
// HandlerThread继承自Thread,内部封装了 Looper
//通过实例化andlerThread新建线程并启动
//所以使用IntentService时不需要额外新建线程
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
//获得工作线程的 Looper,并维护自己的工作队列
mServiceLooper = thread.getLooper();
//将上述获得Looper与新建的mServiceHandler进行绑定
//新建的Handler是属于工作线程的。
mServiceHandler = new ServiceHandler(mServiceLooper);
}
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
//IntentService的handleMessage方法把接收的消息交给onHandleIntent()处理
//onHandleIntent()是一个抽象方法,使用时需要重写的方法
@Override
public void handleMessage(Message msg) {
// onHandleIntent 方法在工作线程中执行,执行完调用 stopSelf() 结束服务。
onHandleIntent((Intent)msg.obj);
//onHandleIntent 处理完成后 IntentService会调用 stopSelf() 自动停止。
stopSelf(msg.arg1);
}
}
////onHandleIntent()是一个抽象方法,使用时需要重写的方法
@WorkerThread
protected abstract void onHandleIntent(Intent intent);
- 总结
从上面源码可以看出,IntentService本质是采用Handler & HandlerThread方式:
通过HandlerThread单独开启一个名为IntentService的线程
创建一个名叫ServiceHandler的内部Handler
把内部Handler与HandlerThread所对应的子线程进行绑定
通过onStartCommand()传递给服务intent,依次插入到工作队列中,并逐个发送给onHandleIntent()
通过onHandleIntent()来依次处理所有Intent请求对象所对应的任务
网友评论