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. MSSQL 导入导出文本文件

    exec master..xp_cmdshell'net use \\192.168.1.8\testfolder "0123" /user:192.168.1.8\wjp' EX ...

  2. jquery-练习-折叠效果

    <!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  3. [汇编语言]-第七章 用[bx+idata]的方式进行数组的处理

    1- 转化为大写 方法一: assume cs:code,ds:data data segment db 'BaSiC' db 'MinIX' data ends code segment start ...

  4. 三角形(hd1249)

    三角形 Problem Description 用N个三角形最多可以把平面分成几个区域?   Input 输入数据的第一行是一个正整数T(1<=T<=10000),表示测试数据的数量.然后 ...

  5. MYSQL 维护表的常用 5 方法

    方法 1. analyze table: 本语句用于分析和存储表的关键字分布.在分析期间,使用一个读取锁定对表进行锁定.这对于MyISAM, BDB和InnoDB表有作用. 方法 2. CHECK T ...

  6. MYSQL select ....outfile.....from.....

    select .... outfile  'file_path' fields terminate by '\t' lines terminate by '\r\n' from table_name; ...

  7. 如何测试一个U盘

    功能测试: 1 在windows xp比较流行的操作系统上是否可以识别(装了驱动后是否可以) 2 在电脑上显示的盘符是否正确 3 总空间,可用空间,已用空间是否显示正确 4 u盘中是否可以拷入各种格式 ...

  8. C11 memory_order

    概念: 摘录自:http://preshing.com/20120913/acquire-and-release-semantics/ Acquire semantics is a property ...

  9. flex——dictionary跟Object的区别与遍历

    AS3中Object和 Dictionary都可以用来保存key-value形式的数据,Dictionary类和Object唯一的区别在于:Dictionary对象可以使用非字符串作为键值对的键.例如 ...

  10. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...