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. oracle 如何搜索当前用户下所有表里含某个值的字段?(转)

    oracle 如何搜索当前用户下所有表里含某个值的字段? create or replace procedure MY_Pro_SearchKeyWord is  v_sql VARCHAR2(400 ...

  2. Linux命令备忘录

    1.tar命令 解压tar.gz格式压缩包:tar zxvf xxx.tar.gz 解压tar.bz2格式压缩包:tar jxvf xxx.tar.bz2 压缩为tar.gz格式压缩包:tar zcv ...

  3. commands - `for`

    internal variable of separator: IFS

  4. jQuery图片懒加载lazyload插件

    http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/ js 模板引擎

  5. 1021 Fibonacci Again (hdoj)

    Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...

  6. 测试框架mochajs详解

    测试框架mochajs详解 章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建 ...

  7. ReactNative

    基于ReactNative实现的博客园手机客户端   去年九月,facebook发布了react-native,将web端的javaScript和react技术扩展到了IOS和Android的原生应用 ...

  8. MySQL应用层传输协议分析

    001.在MySQL应用层传输协议中主要有如下三种类型的数据: 01.整数类型. 02.字符串类型. 03.描述数据包. 002.MySQL应用层传输协议中对整型的说明: 01.固定长度的整型---- ...

  9. MySQL与unix时间问题

    1.select unix_timestamp() -->可以返回以秒记的unix时间. 2.select from_unixtime('1455804595','%Y年%m月%d号'); -- ...

  10. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...