面试之- IntentService的应用

本文详细介绍了Android中的IntentService组件,解析了其内部工作机制,包括如何在后台处理耗时任务,以及如何避免主线程阻塞等问题。此外,还通过一个具体示例展示了IntentService的创建和使用流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不管是何种Service,它默认都是在应用程序的主线程(亦即UI线程)中运行的。所以,如果你的Service将要运行非常耗时或者可能被阻塞的操作时,你的应用程序将会被挂起,甚至会出现ANR错误。为了避免这一问题,你应该在Service中重新启动一个新的线程来进行这些操作。现有两种方法共大家参考:

① 直接在Service的onStartCommand()方法中重启一个线程来执行,如:

  1. @Override  
  2.     public int onStartCommand(Intent intent, int flags, int startId) {  
  3.         MyServiceActivity.updateLog(TAG + " ----> onStartCommand()");  
  4.         new Thread(new Runnable() {  
  5.             @Override  
  6.             public void run() {  
  7.                 // 此处进行耗时的操作,这里只是简单地让线程睡眠了1s  
  8.                 try {  
  9.                     Thread.sleep(1000);  
  10.                 } catch (Exception e) {  
  11.                     e.printStackTrace();  
  12.                 }  
  13.             }  
  14.         }).start();  
  15.         return START_STICKY;  
  16.     }  

 ② Android SDK 中为我们提供了一个现成的Service类来实现这个功能,它就是IntentService,它主要负责以下几个方面:

   

  • Creates a default worker thread that executes all intents delivered to  separate from your application's main thread.

    生成一个默认的且与主线程互相独立的工作者线程来执行所有传送至 onStartCommand() 方法的Intetnt

  • Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.

    生成一个工作队列来传送Intent对象给你的onHandleIntent()方法,同一时刻只传送一个Intent对象,这样一来,你就不必担心多线程的问题。

  • Stops the service after all start requests have been handled, so you never have to call stopSelf().

    在所有的请求(Intent)都被执行完以后会自动停止服务,所以,你不需要自己去调用stopSelf()方法来停止该服务

  • Provides default implementation of onBind() that returns null.

    提供了一个onBind()方法的默认实现,它返回null

  • Provides a default implementation of  that sends the intent to the work queue and then to your onHandleIntent() implementation

    提供了一个onStartCommand()方法的默认实现,它将Intent先传送至工作队列,然后从工作队列中每次取出一个传送至onHandleIntent()方法,在该方法中对Intent对相应的处理.

 

IntentService 执行完onHandleIntent()中的任务后会自动执行onDestroy(); 下次再调用该IntentService,会重新调用onCreate()->onStartCommand()->onHandleIntent()生命周期。

 

IntentService有7个方法,其中最重要的是onHandleIntent(),在这里调用worker线程来处理工作,每次只处理一个intent,像上面描述的,如果有多个,它会顺序处理,直到最后一个处理完毕,然后关闭自己。一点都不用我们操心,多好。

再介绍另一个很有意思的方法,setIntentRedelivery()。从字面理解是设置intent重投递。如果设置为true,onStartCommand(Intent, int, int)将会返回START_REDELIVER_INTENT,如果onHandleIntent(Intent)返回之前进程死掉了,那么进程将会重新启动,intent重新投递,如果有大量的intent投递了,那么只保证最近的intent会被重投递。这个机制也很好,大家可以尝试着用。

 
在Android开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按顺序先后执行,子任务全部执行完后,这项任务才算成功。那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线程必须去手动控制,而且得在一个子线程执行完后,再开启另一个子线程。或者,全部放到一个线程中让其顺序执行。这样都可以做到,但是,如果这是一个后台任务,就得放到Service里面,由于Service和Activity是同级的,所以,要执行耗时任务,就得在Service里面开子线程来执行。那么,有没有一种简单的方法来处理这个过程呢,答案就是IntentService。
 
什么是IntentService,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
 
所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。
 
那么,用IntentService有什么好处呢?首先,我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service。
 
Code                                                                                   
 
接下来让我们来看看如何使用,我写了一个Demo来模拟两个耗时操作,Operation1与Operation2,先执行1,2必须等1执行完才能执行:
 
新建工程,新建一个继承IntentService的类,我这里是IntentServiceDemo.java
 
复制代码
public class IntentServiceDemo extends IntentService {  
  
    public IntentServiceDemo() {  
        //必须实现父类的构造方法  
        super("IntentServiceDemo");  
    }  
      
    @Override  
    public IBinder onBind(Intent intent) {  
        System.out.println("onBind");  
        return super.onBind(intent);  
    }  
  
  
    @Override  
    public void onCreate() {  
        System.out.println("onCreate");  
        super.onCreate();  
    }  
  
    @Override  
    public void onStart(Intent intent, int startId) {  
        System.out.println("onStart");  
        super.onStart(intent, startId);  
    }  
  
  
    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        System.out.println("onStartCommand");  
        return super.onStartCommand(intent, flags, startId);  
    }  
  
  
    @Override  
    public void setIntentRedelivery(boolean enabled) {  
        super.setIntentRedelivery(enabled);  
        System.out.println("setIntentRedelivery");  
    }  
  
    @Override  
    protected void onHandleIntent(Intent intent) {  
        //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务  
        String action = intent.getExtras().getString("param");  
        if (action.equals("oper1")) {  
            System.out.println("Operation1");  
        }else if (action.equals("oper2")) {  
            System.out.println("Operation2");  
        }  
          
        try {  
            Thread.sleep(2000);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
  
    @Override  
    public void onDestroy() {  
        System.out.println("onDestroy");  
        super.onDestroy();  
    }  
  
}
复制代码
我把生命周期方法全打印出来了,待会我们来看看它执行的过程是怎样的。接下来是Activity,在Activity中来启动IntentService:
 
复制代码
public class TestActivity extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        //可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个  
        //Operation 1  
        Intent startServiceIntent = new Intent("com.test.intentservice");  
        Bundle bundle = new Bundle();  
        bundle.putString("param", "oper1");  
        startServiceIntent.putExtras(bundle);  
        startService(startServiceIntent);  
          
        //Operation 2  
        Intent startServiceIntent2 = new Intent("com.test.intentservice");  
        Bundle bundle2 = new Bundle();  
        bundle2.putString("param", "oper2");  
        startServiceIntent2.putExtras(bundle2);  
        startService(startServiceIntent2);  
    }  
}
复制代码
最后,别忘了配置Service,因为它继承于Service,所以,它还是一个Service,一定要配置,否则是不起作用的,开始我就是忘了,结果半天没反应。
 
<service android:name=".IntentServiceDemo">  
            <intent-filter >  
                <action android:name="com.test.intentservice"/>  
            </intent-filter>  
        </service>
最后来看看执行结果:
 
1
 
从结果可以看到,onCreate方法只执行了一次,而onStartCommand和onStart方法执行了两次,开启了两个Work Thread,这就证实了之前所说的,启动多次,但IntentService的实例只有一个,这跟传统的Service是一样的。Operation1也是先于Operation2打印,并且我让两个操作间停顿了2s,最后是onDestroy销毁了IntentService。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值