Android中IntentService与Service
Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独立的线程,它是依赖于应用程序的主线程的,也就是说,在更多时候不建议在Service中编写耗时的逻辑和操作,否则会引起ANR。
那么我们当我们编写的耗时逻辑,不得不被service来管理的时候,就需要引入IntentService,IntentService是继承Service的,那么它包含了Service的全部特性,当然也包含service的生命周期,那么与service不同的是,IntentService在执行onCreate操作的时候,内部开了一个线程,去你执行你的耗时操作。
Service的官方介绍
1.Service不是一个单独的进程 ,它和应用程序在同一个进程中。
2.Service不是一个线程,所以我们应该避免在Service里面进行耗时的操作
IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务。
IntentService有以下特点:
(1) 它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。
(2) 创建了一个工作队列,来逐个发送intent给onHandleIntent()。
(3) 不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。
(4) 默认实现的onBind()返回null
(5) 默认实现的onStartCommand()的目的是将intent插入到工作队列中
继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent()函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.loaderman.intentservicedemo; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService( new Intent( this ,MyService. class )); //主界面阻塞,最终会出现Application not responding //连续两次启动IntentService,会发现应用程序不会阻塞,而且最重要的是第二次的请求会再第一个请求结束之后运行(这个证实了IntentService采用单独的线程每次只从队列中拿出一个请求进行处理) startService( new Intent( this ,MyIntentService. class )); startService( new Intent( this ,MyIntentService. class )); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.loaderman.intentservicedemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; /** * Created by JCF on 2017/2/28. */ public class MyService extends Service { @Override public void onCreate() { super .onCreate(); } @Override public void onStart(Intent intent, int startId) { super .onStart(intent, startId); //经测试,Service里面是不能进行耗时的操作的,必须要手动开启一个工作线程来处理耗时操作 System.out.println( "onStart" ); try { Thread.sleep( 20000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "睡眠结束" ); } @Override public IBinder onBind(Intent intent) { return null ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.loaderman.intentservicedemo; import android.app.IntentService; import android.content.Intent; /** * Created by JCF on 2017/2/28. */ public class MyIntentService extends IntentService { public MyIntentService() { super ( "loaderman" ); } @Override protected void onHandleIntent(Intent intent) { // 经测试,IntentService里面是可以进行耗时的操作的 //IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent //对于异步的startService请求,IntentService会处理完成一个之后再处理第二个 System.out.println( "onStart" ); try { Thread.sleep( 20000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "睡眠结束" ); } } |
本文学习来源:http://blog.csdn.net/smile3670/article/details/7702521
http://www.cnblogs.com/zhangs1986/p/3602154.html
http://blog.csdn.net/matrix_xu/article/details/7974393
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步