Android中Services之异步IntentService
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的更多相关文章
- Android中的AsyncTask异步任务的简单实例
在 Android中的AsyncTask异步任务的简介 一文中.已经对 安卓 异步任务操作做了简单的介绍.这里,直接将上文中的异步任务做了一个实例.实现异步操作更新UI线程,相比开启子线程更新来说逻辑 ...
- Android中Services简析
Services是Android中四大基础组件(Activities. Services. Content Providers. BroadCast Receivers)之一,主要用于在后台长时间运行 ...
- Android中图片的异步加载
转: 1. 为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2. SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...
- Android中的IntentService
首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...
- android 中IntentService的作用及使用
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...
- Android中IntentService与Service
Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独 ...
- android 中IntentService的使用场景
IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行 ...
- Android中的异步网络请求
本篇文章我们来一起写一个最基本的Android异步网络请求框架,借此来了解下Android中网络请求的相关姿势.由于个人水平有限,文中难免存在疏忽和谬误,希望大家可以指出,谢谢大家:) 1. 同步网络 ...
- Android中AsyncTask异步
今天我们学习了 AsyncTack, 这是一个异步任务. 那么这个异步任务可以干什么呢? 因为只有UI线程,即主线程可以对控件进行更新操作.好处是保证UI稳定性,避免多线程对UI同时操作. 同时要把耗 ...
随机推荐
- cobbler配置
:ks脚本关闭pxe,这样就不会重复安装 sed -i 's/pxe_just_once: 0/pxe_just_once: 1/g' /etc/cobbler/settings 6:TFTP服务器 ...
- python-getattr
getattr(object, name[, default]) Return the value of the named attribute of object. name must be a ...
- 初入liunx的一些基本的知识
本系列的博客来自于:http://www.92csz.com/study/linux/ 在此,感谢原作者提供的入门知识 这个系列的博客的目的在于将比较常用的liunx命令从作者的文章中摘录下来,供自己 ...
- 基于jquery的图片轮播 (IE8以上)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 创建寄宿在Windows服务中的WCF服务
1.创建Windows服务项目 2.Server1改名为你想要的名称,比如WinServer 3.在项目中新建一个WCF文件夹,用于存放wcf服务文件. 注:在WcfServer类的上面还要添加 [S ...
- [.NET领域驱动设计实战系列]专题三:前期准备之规约模式(Specification Pattern)
一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题作为一个准备专题,因为在后面一个专题中将会网上书店中的仓储实现引入 ...
- dojo/_base/lang源码分析
dojo/_base/lang模块是一个工具模块,但几乎用dojo开发的app都会用到这个模块.模块中的方法能够在某些开发场景中避免繁冗的代码,接下来我们一起看看这些工具函数的使用和原理(仅仅是原理的 ...
- Dojo动画原理解析
dojo中动画部分分为两部分:dojo/_base/fx, dojo/fx.dojo/_base/fx部分是dojo动画的基石,里面有两个底层API:animateProperty.anim和两个常用 ...
- Window Ghosting
最近工作中遇到Window Ghosting这个问题, 感觉挺有意思,这里简单记录下. 在XP时代我们的程序没有响应后只能通过任务管理器强制杀掉,但是Vista之后情况变了, 我们仍然可以拖动失去响应 ...
- java中基本类型和包装类型实践经验
至今,小菜用java快两年了,有些事,也该有个总结. 基本类型和包装类型的概念在本文不作赘述. 如果这两种类型直接使用,倒没什么值得讨论的,无非就是自动装箱拆箱,java可以让你感觉不到他们的存在,但 ...