Service个人笔记4 --IntentService处理耗时任务

来源:互联网 发布:做网络推广职业前景 编辑:程序博客网 时间:2024/06/02 08:56

本文章中的代码来自 《疯狂Android讲义》

Service可以后台处理一些简单的任务,但是不建议使用Service处理耗时任务。

原因:
1.Service 不会专门启动一条单独的进程,Service与它所在的应用位于同一个进程中。
(Service开启不会启动新线程)
2.Service 本身也不是一条新的线程(Service本身不是新线程)

对于第一个问题,有两种解决方案

    1.在Service中自己另外启动一个线程来处理耗时任务。(可以实现,但存在线程安全问题)    2.在 其他的组件中处理耗时任务,不在Service中完成。(有漏洞)

第1中方法,是可以实现但是当多个异步线程执行时,不能保证线程安全。

第2种方法,是存在缺陷的

    比如你将 耗时任务放在Activity或者BroadcastReceiver中 开启一个子线程 处理。    首先Activity随时会被用户退出,BroadcastReceiver的生命周期也很短。     可能出现的问题是:                   在耗时任务还没有处理完的时候,Activity,或者BroadcastReceiver已经被销毁了。 此时耗    时任务所在的子线程的宿主进程就变成了空进程(没有任何活动组件的进程),系统需要内存的时候会优先终止该进程。    这样就可能导致 任务无法正常完成。                   每次系统的Broadcast事件发生之后,系统会创建对应的BroadcastReceiver的实例,并自动触       发onReceive()方法,onReceiver执行完之后BroadcastReceiver的实例就会被销毁。所以生命周期很短而且如果    BroadcastReceiver的onReceiver()方法不能在10秒钟内执行完,Android会认为程序无响应(ANR),所以耗时    任务是不建议放在onReceive()方法中的

所以Android给我们提供了一个新的Service—->IntentService

IntentService将会使用队列来管理请求Intent,每当客户端代码通过Intent请求启动IntentService时,IntentService会将该Intent加入队列中,然后开启一条新的worker线程来处理Intent。对于异步的startService()请求,IntentService会按次序的一次处理队列中的Intent,该线程保证同一时刻只处理一个Intent。(这样对于多个线程的安全问题就可以不用我们操心了)而且IntentService使用新的worker线程处理Intent请求,因此不会阻塞UI线程。

IntentService特征:

*IntentService会创建单独的worker线程来处理所有的Intent请求。*IntentService会创建单独的worker线程来处理onHandleIntent()方法实现的代码,因此开发者无需处理多线程问题*当所有请求完成之后,IntentService会自动停止,因此开发者无需调用stopSelf()方法来停止该Service*为Service的onBind()方法提供了默认实现,默认实现的onBind()方法返回null。这样就不用自己重写onBind()方法了*为Service的onStartCommand()方法提供了默认实现,该实现会将请求Intent添加到队列中。

因此我们创建一个IntentService的子类的时候只需要重写onHandleIntent()方法就可以 了

定义:IntentService是一个用来在Service中处理耗时任务的封装好了类。
不用考虑,多线程问题,ANR问题。

下面贴出代码,看具体实现过程

Activity实现

public class IntentServiceTest extends Activity{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    public void startService(View source)    {        // 创建需要启动的Service的Intent        Intent intent = new Intent(this, MyService.class);        // 启动Service        startService(intent);    }    public void startIntentService(View source)    {        // 创建需要启动的IntentService的Intent        Intent intent = new Intent(this, MyIntentService.class);        // 启动IntentService        startService(intent);    }}

IntentService实现

public class MyIntentService extends IntentService{    public MyIntentService()    {        super("MyIntentService");    }    // IntentService会使用单独的线程来执行该方法的代码    @Override    protected void onHandleIntent(Intent intent)    {        // 该方法内可以执行任何耗时任务,比如下载文件等,此处只是让线程暂停20秒        long endTime = System.currentTimeMillis() + 20 * 1000;        System.out.println("onStart");        while (System.currentTimeMillis() < endTime)        {            synchronized (this)            {                try                {                    wait(endTime - System.currentTimeMillis());                }                catch (Exception e)                {                }            }        }        System.out.println("---耗时任务执行完成---");    }}

普通Service实现

public class MyService extends Service{    @Override    public IBinder onBind(Intent intent)    {        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId)    {        // 该方法内执行耗时任务可能导致ANR(Application Not Responding)异常        long endTime = System.currentTimeMillis() + 20 * 1000;        System.out.println("onStart");        while (System.currentTimeMillis() < endTime)        {            synchronized (this)            {                try                {                    wait(endTime - System.currentTimeMillis());                }                catch (Exception e)                {                }            }        }        System.out.println("---耗时任务执行完成---");        return START_STICKY;    }}
0 0