1.为什么需要IntentService

是LocalService的包装类,简便Service的创建,使用的是startService(),也就是访问者退出Service不会消失。

2.实现原理

步骤一:

public FirstService extends IntentService{
public FirstService (String name){
super(name);//需要为该Service命名
} @Override
protected void onHandleIntent(Intent intent) {
//用来实现的方法的地方
}
}

步骤二:在AndroidManifest.xml中注册Service

<Service android:name = ".FirstService">
</Service>

步骤三:创建Intent信息发送给Service。

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,FirstService.class);
startService(intent);//将intent发送给Service
}
}

原理:当Service第一次接收到intent的时候,IntentService完成启动,触发一个后台线程,将intent放入队列尾部。然后在后台线程上逐个调用队列的intent触发onHandleIntent(Intent)方法。

IntentService的使用的更多相关文章

  1. 什么时候用IntentService

    IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...

  2. IntentService

    http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...

  3. 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的

    稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...

  4. HandlerThread和IntentService

    HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...

  5. android 中IntentService的作用及使用

    IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...

  6. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...

  7. Android中Services之异步IntentService

    IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1)  它创 ...

  8. IntentService源码分析

    和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...

  9. Android Service 与 IntentService

    Service 中的耗时操作必须 在 Thread中进行: IntentService 则直接在 onHandleIntent()方法中进行

  10. Android IntentService完全解析 当Service遇到Handler

    一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...

随机推荐

  1. 在C#实现托盘效果(转)

    桌面程序的开发中,经常考虑能在状态栏实现托盘快捷操作,托盘程序的实现在API时代,还是相对复杂的,首先在MSDN中可以查看其函数细节, 然后在根据其要求的参数进行复杂的设置.      在.NET时代 ...

  2. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  3. 一段简单c程序的汇编语言学习(ubuntu+x86)

    c程序代码: #include <stdio.h> int main(void) { int i=0, j=0; for(i=0; i<8; i++) j=j+1; return 0 ...

  4. Android 底部Dialog显示

    public void showComplainDialog() { ComplainDialog complain_dialog = new ComplainDialog(OrderDetialAc ...

  5. fltk demo

    背景: 基于microwindows nano-X上面运行fltk demo,是nano-X API 预留的fltk接口. 运行环境及程序版本: microwindows-0.89pre8 (源文件必 ...

  6. Python 读取csv文件到excel

    朋友问我如何通过python把csv格式的文件另存为xls文件,自己想了想通过读取csv文件然后再保存到xls文件中即可,也许还有其他简单的方法,但这里也为了练习python语法及其他知识,所以采用了 ...

  7. JavaBean之属性必须遵循命名规范

    JavaBean中存在一些属性,今天写了一个Jsp的小例子.把Bean中的属性命名规范搞错了,具体的说属性的首字母大写了. 于是乎Jsp中读取属性时总是报错. javax.el.PropertyNot ...

  8. SQL Server 查看表定义的 2 种方法

    方法 1. 用SQL Server Management Studio 第一步找到要查看的表,右键 第二步点设计 方法 2. sp_help @objname = 'tableName' execut ...

  9. 关于C#的一点小知识 以后自己用

    项目过程中遇到C#代码的编写 上网查之后的结果 @html.ActionLink的几种参数格式 一 Html.ActionLink("linkText","actionN ...

  10. Oracle EBS-SQL (MRP-6):检查MRP计划运行报错原因之超大数据查询1.sql

    /*逐一运行检查计划运行超大数据*/ ---------------------------------------------------- /*查询-1*/ select  plan_id, 'C ...