当前位置: 首页 > news >正文

莘县网站制作/网络seo优化

莘县网站制作,网络seo优化,网站access数据怎么做,wordpress评论框中加文字提示IntentService是一个带线程的service,用于处理Intent类型的异步任务请求。当客户端调用startService(Intent)发送请求时,Service服务被启动,且在其内部构建一个工作线程来处理Intent请求。当工作线程执行结束,Service服务会自动停…

IntentService是一个带线程的service,用于处理Intent类型的异步任务请求。当客户端调用startService(Intent)发送请求时,Service服务被启动,且在其内部构建一个工作线程来处理Intent请求。当工作线程执行结束,Service服务会自动停止。IntentService继承于Service,它最大的特点是对服务请求逐个进行处理。当我们要提供的服务不需要同时处理多个请求的时候,可以选择继承IntentService。IntentService是一个抽象类,用户必须实现一个子类去继承它,且必须至少要实现两个函数:构造函数和onHandleIntent()函数。


IntentService源码分析

/** Copyright (C) 2008 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.app;import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;/*** IntentService is a base class for {@link Service}s that handle asynchronous* requests (expressed as {@link Intent}s) on demand.  Clients send requests* through {@link android.content.Context#startService(Intent)} calls; the* service is started as needed, handles each Intent in turn using a worker* thread, and stops itself when it runs out of work.** <p>This "work queue processor" pattern is commonly used to offload tasks* from an application's main thread.  The IntentService class exists to* simplify this pattern and take care of the mechanics.  To use it, extend* IntentService and implement {@link #onHandleIntent(Intent)}.  IntentService* will receive the Intents, launch a worker thread, and stop the service as* appropriate.** <p>All requests are handled on a single worker thread -- they may take as* long as necessary (and will not block the application's main loop), but* only one request will be processed at a time.** <div class="special reference">* <h3>Developer Guides</h3>* <p>For a detailed discussion about how to create services, read the* <a href="{@docRoot}guide/topics/fundamentals/services.html">Services</a> developer guide.</p>* </div>** @see android.os.AsyncTask*/
public abstract class IntentService extends Service {private volatile Looper mServiceLooper;private volatile ServiceHandler mServiceHandler;private String mName;private boolean mRedelivery;
/*ServiceHandler是IntentService的内部类,继承自Handler,在重写消息处理方法handlerMessage里面调用了onHandlerIntent
抽象方法去处理异步任务intent的请求,当异步任务请求结束之后,调用stopSelf方法自动结束IntentService服务。此处handleMessage
方法是在工作线程中调用的,因此我们子类重写的onHandlerIntent
也是在工作线程中实现的。
*/private final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {onHandleIntent((Intent)msg.obj);stopSelf(msg.arg1);}}/*** Creates an IntentService.  Invoked by your subclass's constructor.** @param name Used to name the worker thread, important only for debugging.*///IntentService构造方法,参数name用于定义工作线程的名称public IntentService(String name) {super();mName = name;}/*** Sets intent redelivery preferences.  Usually called from the constructor* with your preferred semantics.** <p>If enabled is true,* {@link #onStartCommand(Intent, int, int)} will return* {@link Service#START_REDELIVER_INTENT}, so if this process dies before* {@link #onHandleIntent(Intent)} returns, the process will be restarted* and the intent redelivered.  If multiple Intents have been sent, only* the most recent one is guaranteed to be redelivered.** <p>If enabled is false (the default),* {@link #onStartCommand(Intent, int, int)} will return* {@link Service#START_NOT_STICKY}, and if the process dies, the Intent* dies along with it.*/public void setIntentRedelivery(boolean enabled) {mRedelivery = enabled;}//onCreate方法@Overridepublic void onCreate() {// TODO: It would be nice to have an option to hold a partial wakelock// during processing, and to have a static startService(Context, Intent)// method that would launch the service & hand off a wakelock.//利用HandlerThread类创建了一个循环的工作线程thread,然后将工作线程中的Looper对象作为参数来创建ServiceHandler消息执行者。super.onCreate();HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");thread.start();mServiceLooper = thread.getLooper();mServiceHandler = new ServiceHandler(mServiceLooper);}/*该方法中通过mServiceHandler获得一个消息对象msg,然后将startId作为该消息的消息码,将异步任务请求intent作为消息内容封装成一个消息msg发送到mServiceHandler消息执行者中去处理.*/@Overridepublic void onStart(Intent intent, int startId) {Message msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.obj = intent;mServiceHandler.sendMessage(msg);}/*** You should not override this method for your IntentService. Instead,* override {@link #onHandleIntent}, which the system calls when the IntentService* receives a start request.* @see android.app.Service#onStartCommand*///Service服务生命周期第二步执行onStartCommand方法。@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {onStart(intent, startId);return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}
/*  该方法中调用HandlerThread工作线程中Looper对象的quit方法让当前工作线程HandlerThread退出当前Looper循环,进而结束线程。
进而结束当前IntentService服务。
*/@Overridepublic void onDestroy() {mServiceLooper.quit();}/*** Unless you provide binding for your service, you don't need to implement this* method, because the default implementation returns null. * @see android.app.Service#onBind*/@Overridepublic IBinder onBind(Intent intent) {return null;}/*** This method is invoked on the worker thread with a request to process.* Only one Intent is processed at a time, but the processing happens on a* worker thread that runs independently from other application logic.* So, if this code takes a long time, it will hold up other requests to* the same IntentService, but it will not hold up anything else.* When all requests have been handled, the IntentService stops itself,* so you should not call {@link #stopSelf}.** @param intent The value passed to {@link*               android.content.Context#startService(Intent)}.*//* 该方法用于处理intent异步任务请求,在工作线程中调用该方法。每一个时刻只能处理一个intent请求,当同时又多个intent请求时,也就是客户端下一个intent请求。直到所有的intent请求结束之后,IntentService服务会调用stopSelf停止当前服务。也就是当intent异步任务处理结束之后,对应的IntentService服务会自动销毁
*/protected abstract void onHandleIntent(Intent intent);
}

可以看出来,IntentService的核心就是HandlerThread源码分析,HandlerThread+Handler构建成了一个带有消息循环机制的异步任务处理机制。只要搞明白了HandlerThread,自然而然就明白IntentService了。


IntentService总结

IntentService有以下特点:
(1) 它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。
(2) 创建了一个工作队列,来逐个发送intent给onHandleIntent()。
(3) 不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。
(4) 默认实现的onBind()返回null
(5) 默认实现的onStartCommand()的目的是将intent插入到工作队列中。
只要我们的Service继承IntentService,实现onHandleIntent()就可以工作在非主线程,而且还不用担心并发,不用担心关闭service等问题。
IntentService类内部利用HandlerThread+Handler构建了一个带有消息循环处理机制的后台工作线程,这是一个单线程来处理异步任务。客户端只需调用startService(Intent)将Intent任务请求放入后台工作队列中。只要当前IntentService服务没有被销毁,客户端就可以同时投放多个Intent异步任务请求,IntentService服务端这边是顺序执行当前后台工作队列中的Intent请求的,也就是每一时刻只能执行一个Intent请求,直到该Intent处理结束才处理下一个Intent。

http://www.lbrq.cn/news/1552015.html

相关文章:

  • wordpress4.9.5/东莞网络推广及优化
  • 网站制作内容文案/怎么在百度做宣传广告
  • 做网站大约要多少钱/更厉害的病毒2024
  • 微信小程序网站模板/搜狗友链交换
  • 做电影网站哪个系统好/电子商务网站建设论文
  • 网站开发是什么意思啊/百度学术论文查重
  • 四川住房和建设厅官网/seo和竞价排名的区别
  • 邯郸网站建设哪能做/aso优化师工作很赚钱吗
  • 网站编辑软件有哪些/优就业seo课程学多久
  • 黄冈做网站/企业网站怎么做
  • 怎样把已经有的网站做推广/爱站网的关键词是怎么来的
  • 网站开发流程比较合理/广东深圳疫情最新
  • 网站优化找谁/足球世界积分榜
  • wordpress显示摘要插件/seo搜索引擎优化介绍
  • 网页制作做网站左侧导航/东莞今日头条新闻
  • 网站建设方案机构/哪家建设公司网站
  • 织梦网站图片无缝滚动怎么做/全网关键词云在哪里看
  • 便宜做网站/百度seo排名点击软件
  • 杭州做网站的优质公司哪家好/承德网络推广
  • 专业做互联网招聘的网站/营销的手段和方法
  • 大港做网站公司/大数据查询
  • 机械建设网站/可以免费发广告的网站
  • 做手机网站优/引擎搜索有哪些
  • 没有服务器怎么先做网站/推广app赚佣金
  • 广告在什么网站做/有趣的软文
  • 谁做的怀来吧网站/营销网站系统
  • 山东做网站找谁/今日疫情最新消息
  • wordpress插件怎么安/seo的搜索排名影响因素有
  • 再网站里做商家店铺/seo软件推广哪个好
  • 网站目录结构说明/个人网站模板建站
  • elasticsearch-集成prometheus监控(k8s)
  • go语言条件语if …else语句
  • 《WINDOWS 环境下32位汇编语言程序设计》第3章 使用MASM
  • 基于51单片机汽车自动照明灯超声波光敏远近光灯设计
  • 文件快速复制工具,传输速度提升10倍
  • 【递归、搜索与回溯算法】记忆化搜索