IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务。

IntentService有以下特点:

(1)  它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。

(2)  创建了一个工作队列,来逐个发送intent给onHandleIntent()。

(3)  不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。

(4)  默认实现的onBind()返回null

(5)  默认实现的onStartCommand()的目的是将intent插入到工作队列中

继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent()函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数。

界面设置两按钮:

 <Button
android:id="@+id/btnStartIntentService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始下载" >
</Button> <Button
android:id="@+id/btnStopIntentService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结束下载" >
</Button>

声明IntentServiceSub继承IntentService

public class IntentServiceSub extends IntentService {

    private static final String TAG = "IntentServiceSub";

    public IntentServiceSub() {
super("IntentServiceSub");
Log.i(TAG, "=>IntentServiceSub");
} /* (non-Javadoc)
* @see android.app.IntentService#onCreate()
*/
@Override
public void onCreate() {
Log.i(TAG, "=>onCreate");
super.onCreate();
} /* (non-Javadoc)
* @see android.app.IntentService#onDestroy()
*/
@Override
public void onDestroy() {
Log.i(TAG, "=>onDestroy");
super.onDestroy();
} @Override
protected void onHandleIntent(Intent arg0) {
Log.i(TAG, "IntentService 线程:"+Thread.currentThread.getId());
Thread.sleep(2000);
}

页面按钮事件

        btnStartIntentService = (Button) this.findViewById(R.id.btnStartIntentService);
btnStopIntentService = (Button) this.findViewById(R.id.btnStopIntentService); private OnClickListener listener = new OnClickListener() { @Override
public void onClick(View v) { case R.id.btnStartIntentService:
Log.i(TAG, "主线程ID:"+Thread.currentThread.getId());
if (mServiceIntent == null)
mServiceIntent = new Intent(AndroidServiceActivity.this,IntentServiceSub.class);
startService(mServiceIntent);
break;
case R.id.btnStopIntentService:
Log.i(TAG, "btnStopIntentService");
if (mServiceIntent != null) {
stopService(mServiceIntent);
mServiceIntent = null;
}
break;
} }
};

Android中Services之异步IntentService的更多相关文章

  1. Android中的AsyncTask异步任务的简单实例

    在 Android中的AsyncTask异步任务的简介 一文中.已经对 安卓 异步任务操作做了简单的介绍.这里,直接将上文中的异步任务做了一个实例.实现异步操作更新UI线程,相比开启子线程更新来说逻辑 ...

  2. Android中Services简析

    Services是Android中四大基础组件(Activities. Services. Content Providers. BroadCast Receivers)之一,主要用于在后台长时间运行 ...

  3. Android中图片的异步加载

    转: 1.  为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2.  SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...

  4. Android中的IntentService

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

  5. android 中IntentService的作用及使用

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

  6. Android中IntentService与Service

    Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独 ...

  7. android 中IntentService的使用场景

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

  8. Android中的异步网络请求

    本篇文章我们来一起写一个最基本的Android异步网络请求框架,借此来了解下Android中网络请求的相关姿势.由于个人水平有限,文中难免存在疏忽和谬误,希望大家可以指出,谢谢大家:) 1. 同步网络 ...

  9. Android中AsyncTask异步

    今天我们学习了 AsyncTack, 这是一个异步任务. 那么这个异步任务可以干什么呢? 因为只有UI线程,即主线程可以对控件进行更新操作.好处是保证UI稳定性,避免多线程对UI同时操作. 同时要把耗 ...

随机推荐

  1. chrome插件开发-消息机制中的bug与解决方案

    序言 最近开发chrome插件,涉及到消息传递机时按照教程去敲代码,结果总是不对.研究了大半天终于找到原因,现在记录下. 程序 插件程序参考官网 chrome官网之消息传递机制, 不能FQ的同事也可以 ...

  2. Java使用velocity导出word

    效果展示: 使用word编辑好模板

  3. highcharts 当Y轴全部没有数据的时候 数据标签显示最下面 而不是居中显示

    yAxis: {min: 0,minRange: 1}

  4. Win10 无法完全关机问题

    Win10是重新安装的.开机运行时间长了或者跑的东西多了.关机,键盘灯还是亮的,要强制按电源键关机才行. 问题解决:从网上搜以为是显卡驱动问题,上官网更新最新驱动,结果还是关不了机.偶然间搜到是Int ...

  5. 跨平台开源通讯组件elastic communication

    elastic communication是基于c#开发支持.net和mono的通讯组件(简称EC),EC的主要目的简化mono和.net下的通讯开发难度,通过EC可以非常快速地开发基于mono和.n ...

  6. MQTT V3.1--我的理解

    最近因为工作需要,需要对推送消息了解,因此对MQTT进行了整理,这里更多的是对MQTT英文版的翻译和理解. MQTT(Message Queue Telemetry Transport),遥测传输协议 ...

  7. [Asp.net 开发系列之SignalR篇]专题一:Asp.net SignalR快速入门

    一.前言 之前半年时间感觉自己有点浮躁,导致停顿了半年多的时间没有更新博客,今天重新开始记录博文,希望自己可以找回初心,继续沉淀.由于最近做的项目中用到SignalR技术,所以打算总结下Asp.net ...

  8. 【腾讯优测干货】看腾讯的技术大牛如何将Crash率从2.2%降至0.2%?

    小优有话说: App Crash就像地雷. 你怕它,想当它不存在.无异于让你的用户去探雷,一旦引爆,用户就没了. 你鼓起勇气去扫雷,它却神龙见首不见尾. 你告诫自己一定开发过程中减少crash,少埋点 ...

  9. AMD加载器实现笔记(四)

    继续这一系列的内容,到目前为止除了AMD规范中config的map.config参数外,我们已经全部支持其他属性了.这一篇文章中,我们来为增加对map的支持.同样问题,想要增加map的支持首先要知道m ...

  10. JavaScript使用DeviceOne开发实战(六)点墨真实案例

    qq群里的yan用户开发的App,基本完工大家可以看看 安装二维码是 QQ群:365443130