阆中网站建设,深圳向失业人员发放补贴,设计公司是建筑企业吗,青岛黄岛网站建设公司电话在Android的四大组件中#xff0c;Service排行老二#xff0c;在Android中的主要作用是后台服务#xff0c;进行与界面无关的操作。由于Service运行在主线程#xff0c;所以进行异步操作需要在子线进行。为此Android为我们提供了IntentService。 IntentService是一个抽象类…在Android的四大组件中Service排行老二在Android中的主要作用是后台服务进行与界面无关的操作。由于Service运行在主线程所以进行异步操作需要在子线进行。为此Android为我们提供了IntentService。 IntentService是一个抽象类继承至Service主要方便我们新建工作线程进行异步操作。提交任务到IntentService时异步任务以串行方式进行处理意味着工作线程一次只处理一个任务。而且当所有任务都完成之后会自动停止Service不需要我们手动停止。 IntentService 的使用 我们定义DownloadService类并继承至IntentService。来模拟网络下载的过程。public class DownloadService extends IntentService {private static int count 0;/*** 主要用于调用服务类构造器** param name 用于区分不同任务*/public DownloadService(String name) {super(name);}/*** AndroidManifest.xml配置清单需要配置** param*/public DownloadService() {super(action);}/***主要重写该方法在该方法内进行异步操作。**/Overrideprotected void onHandleIntent(Intent intent) {Log.i(Download, onHandleIntent count);count;String name intent.getStringExtra(action);if (name.equals(download)) {for (int i 0; i 5; i) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();return;}Log.i(Download, download: count);}}}//以下方法的重写仅仅为了打印日志Overridepublic void onDestroy() {super.onDestroy();Log.i(Download, onDestroy);}Overridepublic void onCreate() {super.onCreate();Log.i(Download, onCreate);}Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Log.i(Download, onStart);}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(Download, onStartCommand);return super.onStartCommand(intent, flags, startId);}
}复制代码在AndroidManifest.xml配置DownloadService。service android:name.DownloadService/
复制代码在MainActivity类中循环调用Service启动多循环任务。 Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intentnew Intent(this,DownloadService.class);for (int i 0;i3;i){intent.putExtra(action,download);intent.putExtra(count,i);startService(intent);}}
复制代码运行结果 分析 从运行结果知道IntentService在运行多个任务情况下只调用一次onCreate,调用多次onStartCommand,跟Service的生命周期一致。但只有在运行完download1之后才会去运行download2接着是download:3。最后所有任务结束后会自动调用onDestroy,停止服务。在这里需要注意的是和Service并不同Service需要我们手动停止服务。对于结果的回调可以采用接口回调广播EventBus。 那么IntentService是如何在Service中实现异步操作和串行处理任务的呢 IntentService内部实现 查看IntentService的onCreate方法 Overridepublic void onCreate() {super.onCreate();//分析一HandlerThread thread new HandlerThread(IntentService[ mName ]);thread.start();mServiceLooper thread.getLooper();分析二mServiceHandler new ServiceHandler(mServiceLooper);}
复制代码分析一 HandThread继承Thread通过start方法创建工作线程内部建立Looper来达到消息循环通过Hanlder消息机制来达到串行的效果和处理多任务。HandThread和Handler消息机制可以另外查看文章。 分析二 ServiceHandler继承Handler与普通的Handler并没有区别在其内容处理handleMessage。即调用IntentService的onHandleIntent private final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) {super(looper);}Overridepublic void handleMessage(Message msg) {onHandleIntent((Intent)msg.obj);stopSelf(msg.arg1);}}
复制代码那么当我们在Activity中重复调用startService方法时只会多次调用onStartCommand方法,并不会重复调用onCreate方法。我们看看onStartComamnd方法的实现。 Overridepublic int onStartCommand(Nullable Intent intent, int flags, int startId) {onStart(intent, startId);return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}
复制代码可以看得出调用了onStart方法了。而onStart方法只是将我们的Intent包装成Message通过Handler发送出去并在Handler中进行处理调用我们的onHandleIntent。进而调用我们实现onHandleIntent的代码。 Overridepublic void onStart(Nullable Intent intent, int startId) {Message msg mServiceHandler.obtainMessage();msg.arg1 startId;msg.obj intent;mServiceHandler.sendMessage(msg);}
复制代码总结 IntentService并没有什么的新的技术含量在了解HandlerTread和Handler的消息机制下对HandlerThreadService作一个封装更适合我们后台进行异步耗时操作的场景。有效避免通过new多个Thread。 知识点分享 Android消息机制 HandlerThread必知必会 如果觉得文章有用给文章点个赞铁子