Android Service与IntentService区别

相同点:

1、首先Service与IntentService都是Android的基本组件service

2、使用时都是一样需要创建,配置;和调用启动方式都是一样的

不同点:

1、IntentService是继承自Service的service 类,创建了自己的特有方法onHandleIntent——这个方法的主要作用是:

而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个

多次启动IntentService时onHandleIntent运行如下——类似同步synchronization的作用

 2、每次IntentService执行完任务都会自动停止,而不需要我们手动去控制或stopSelf()。

IntentService的onDestroy方法

@Override
public void onDestroy() {
    mServiceLooper.quit();
}

Service的onDestroy方法

public void onDestroy() {
}

3、IntentService每次执行都开启一个子线程

 同样打印线程

IntentService运行结果如下:

Log.i("lgqonHandleIntent--", i + "--" + Thread.currentThread().getName());

04-23 09:50:45.176 12553-13556/com.tianxin.ttttest I/lgqonHandleIntent--: 0--IntentService[MIntentService]

Service运行结果如下:

Log.i("lgqq","body==22===onCreate-----"+Thread.currentThread().getName());

04-23 09:52:14.722 12553-12553/com.tianxin.ttttest I/lgqq: body==22===onCreate-----main

IntentService在onStart方法调用onHandleIntent方法

 

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/89466052
今日推荐