美文网首页
Android初级开发笔记 - IntentService

Android初级开发笔记 - IntentService

作者: 8e750c8f0fae | 来源:发表于2019-09-26 08:12 被阅读0次

    @[toc]

    一、定义及作用

    IntentService 是继承于Service,用于处理异步请求,实现多线程的一个类。

    二、使用及原理

    如何使用:
    步骤1:定义IntentService的子类:传入线程名称、复写onHandleIntent()方法

    package com.example.administrator.test;
    
    import android.app.IntentService;
    import android.content.Intent;
    import android.util.Log;
    
    public class myIntentService extends IntentService {
    
        public myIntentService() {
            //构造函数参数=工作线程的名字
            super("myIntentService");
        }
    
        /*复写onHandleIntent()方法*/
        //实现耗时任务的操作
        @Override
        protected void onHandleIntent(Intent intent) {
            //不同的事务用不同的ntent标记
            String taskName = intent.getExtras().getString("taskName");
            switch (taskName) {
                case "task1":
                    Log.i("myIntentService", "do task1");
                    break;
                case "task2":
                    Log.i("myIntentService", "do task2");
                    break;
                default:
                    break;
            }
        }
    
    
        @Override
        public void onCreate() {
            Log.i("myIntentService", "onCreate");
            super.onCreate();
        }
    
        /*复写onStartCommand()方法*/
        //默认实现将请求的Intent添加到工作队列里
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("myIntentService", "onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            Log.i("myIntentService", "onDestroy");
            super.onDestroy();
        }
    }
    

    步骤2:在Manifest.xml中注册服务

    <service android:name=".myIntentService"/>
    

    步骤3:在Activity中开启Service服务

    package com.example.administrator.test;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
            //同一服务只会开启一个工作线程
            //在onHandleIntent函数里依次处理intent请求。
    
            Intent i = new Intent("cn.scu.finch");
            Bundle bundle = new Bundle();
            bundle.putString("taskName", "task1");
            i.putExtras(bundle);
            startService(i);
    
            Intent i2 = new Intent("cn.scu.finch");
            Bundle bundle2 = new Bundle();
            bundle2.putString("taskName", "task2");
            i2.putExtras(bundle2);
            startService(i2);
    
            startService(i);  //多次启动
        }
    }
    

    为何能成:
    因为IntentService本质 = Handler + HandlerThread:

    (1)通过HandlerThread 单独开启1个工作线程
    (2)该线程继承了Thread,封装了Looper
    (3)创建1个内部 Handler :ServiceHandler
    (4)绑定 ServiceHandler 与 Looper
    (5)通过 onStartCommand() 传递服务intent 到ServiceHandler 、依次插入Intent到工作队列中 & 逐个发送给 onHandleIntent()
    (6)通过onHandleIntent() 依次处理所有Intent对象所对应的任务

    四、对比Service以及后台线程

    • IntentService与Service的区别
      • Service:不能直接进行耗时操作(会ANR),如果有耗时操作就必须开启一个单独的线程来处理。IntentService不用再单独开线程,因为其内部已经开了工作线程进行处理。并且在处理完成后会自动停止服务。欸~这里就体现好处了,service需要调用stopSelf()或者stopService()停止服务,那么就会出现有时候忘记停止的情况,但是使用IntentService就不用担心会出现这个问题
      • Service需要主动调用stopSelft()来结束服务,而IntentService不需要(在所有intent被处理完后,系统会自动关闭服务)
    • IntentService与线程的区别
      • IntentService:能处理想要顺序执行的耗时操作,反之,无法处理同时进行的耗时操作
      • 后台线程:优先级没有IntentService高,毕竟人家IntentService是继承Service,在资源紧张的情况下,相对来说后台线程比较容易被消灭掉

    五、使用场景

    • 线程任务需要按顺序、在后台执行的使用场景
      最常见的场景:离线下载
    • 由于所有的任务都在同一个Thread looper里面来做,所以不符合多个数据同时请求的场景。

    内推信息

    相关文章

      网友评论

          本文标题:Android初级开发笔记 - IntentService

          本文链接:https://www.haomeiwen.com/subject/gprguctx.html