- 浏览: 47640 次
- 性别:
- 来自: 西安
最新评论
下面是android Service的高级应用的一个例子,主要内容是在Service中spawn一个thread 来执行一写 CPU intensive的任务,以便让UI thread 能够更好的运行。
这里我们使用到了Looper 和 Handler 两个特殊的类。
Looper 是一个用来运行一个线程的Message循环 ,每一个线程都默认的有一个和他们相关联的Looper。
Handler 是Looper 用来处理这个线程的消息循环的类
整个流程的概括,调用Service的client 用Itent来调用这个Service,然后就会运行public void onStart(Intent intent, int startId), 我们通过Intent中不同的action 来确定client想要完成的工作。 根据不同的action给thread的消息队列发送不同的消息
Message msg = Message.obtain();
msg.what = MSG_UPDATE;
handler.sendMessage(msg);
下面是实现的详细代码
public class MyService extends Service {
private static final int MSG_UPDATE= 0;
private static final int MSG_CANCLE = 1;
/**
* intent to update
*/
public static final String ACTION_UPDATE = "UPDATE";
/**
* intent to cancel */
public static final String ACTION_CANCLE = "CANCLE";
private volatile Looper mServiceLooper;
private volatile ServiceHandler handler;
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case MSG_UPDATE:
Log.v("TAG", "Update");
break;
case MSG_CANCLE:
Log.v("TAG", "Cancle");
break;
}
};
@Override
public void onCreate() {
super.onCreate();
log.info("ReadingService.onCreate()");
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("service",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
//获取thread默认的Looper
mServiceLooper = thread.getLooper();
//用Looper来构造这个 Handler 这样的话 Looper就可以用这个Handler来处理thread的消息循环
handler = new ServiceHandler(mServiceLooper);
}
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
//@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return 1; // START_STICKY
}
private void handleCommand(Intent intent) {
if (intent == null || (intent.getAction() == null)) {
return;
}
String action = intent.getAction();
if (ACTION_UPDATE.equals(action)) {
//Return a new Message instance from the global pool
Message msg = Message.obtain();
//Message还可以携带信息
msg.setData(intent.getExtras());
msg.what = MSG_UPDATE;
handler.sendMessage(msg);
} else if (ACTION_CANCLE.equals(action)) {
//从消息队列中删掉消息
handler.removeMessages(MSG_UPDATE);
}
}
@Override
public void onDestroy() {
//退出消息循环
mServiceLooper.quit();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
发表评论
-
Android Intent and Intent Filter (转)
2011-03-10 22:47 1459Android Intent and Intent ... -
Intent详解
2011-03-03 14:18 1141在应用中,我们可以以两种形式来使用Intent: 直接I ... -
Intent间传送数据一般有两种常用方法
2011-01-24 14:50 1306Intent间传送数据一般有两种常用的办法: 1.extra ... -
android RelativeLayout 详解
2011-01-01 09:46 1580<?xml version="1.0" ... -
How to Use Android Downloads Provider
2010-12-20 21:41 1330本文转载自 http://blog.lytsing.or ... -
android 开发中的一些小知识点
2010-12-17 09:49 9991 关于onPause(): onPause ... -
content provider 深入解析
2010-12-15 22:04 1098Content providers are on ... -
TabActivity
2010-12-12 19:09 1772下面是一个使用android tabactivity 的例子 ... -
android 测试初探(android test)
2010-12-07 15:00 8015android Testing and Instrumenta ... -
Localization of android
2010-12-05 21:51 0android 会在不同地域的不同机器上运行。为了是应用能够 ... -
android高效编程之使用本地变量
2010-12-05 14:26 975hava a look at the following co ... -
在不同的Activity中传递对象的方法
2010-12-02 10:20 2011下面我们将要实现的功 ... -
Cursor与Adapter
2010-11-21 08:34 1226来自: http://hi.baidu.com/lfcaoli ... -
Working with Context Menus
2010-11-15 23:36 860... -
Handler的理解
2010-11-14 20:58 836A Handler allows you to sen ... -
android开发问题解决日志
2010-11-14 18:33 18791.、android中R文件消失的处理方式 ... -
android 中对SDCard 的操作
2010-11-11 14:14 1636对于像视频这样的大文件,我们可以把它存放在SDCard。 SD ... -
ViewAnimation
2010-11-05 12:46 1039view animation 的实现 Understandi ... -
android高效的编写高效代码
2010-10-17 19:13 788本文来源于: 从此学习网 原文: http://www.con ... -
scale animation
2010-10-11 23:29 1610Scale animation: You use this t ...
相关推荐
在“Android_Service的高级应用示例”中,我们将深入探讨Service组件的多种高级用法,包括IntentService、Foreground Service以及它们在实际应用中的重要性。 首先,我们来看IntentService。IntentService是Service...
Java是一种广泛使用的编程语言,尤其在构建高级应用方面表现出色。本课程主要涵盖了基于Java的Web数据库访问技术、Web编程动态技术,包括JSP(Java Server Pages)和Servlet,以及高级数据库设计和基于MVC(Model-...
### SQL Server 2005 Reporting Service 特性与高级应用 #### 一、概述 SQL Server 2005 Reporting Services(以下简称“Reporting Services”)是微软为满足企业级报表需求而推出的一个完整的报表解决方案。它...
本示例“Android高级应用源码-app更新,实现service下载.zip”提供了一个使用服务(Service)进行后台下载更新的解决方案。以下是关于这个主题的详细知识点: 1. **Android Service**: 在Android系统中,Service是...
**Java Web之高级应用** Java Web技术是互联网应用程序开发的核心之一,它涵盖了各种技术和框架,用于构建动态、交互式的Web应用程序。在这个高级应用的主题中,我们主要探讨的是Java Web技术的深入理解和实践,由...
本资源包含PPT、视频和文档,提供了对SQL Server 2005 Reporting Service特性和高级应用的深入讲解。 1. **Reporting Services基础** SQL Server 2005 Reporting Services(简称SSRS)是一个服务器端报告解决方案...
本资源提供了一个基于Android开发的高级应用源码,是一个简单的音乐播放器,它实现了音乐播放器的基本功能,并且能够通过Service在后台持续播放音乐。这个项目对于那些希望深入学习Android应用开发,特别是音乐...
三、PB11 Web Service的高级特性 1. **错误处理**:PB11提供了一套完整的错误处理机制,允许开发者捕获和处理Web Service调用过程中可能出现的问题。 2. **数据映射**:PB11允许开发者定义数据类型与Web Service...
内含下列VS2005高级组件教程 VS高级组件应用之一 ...VS高级控件应用之六 DirectoryEntry VS高级控件应用之七 FileSystemWatcher VS高级控件应用之八 Process VS高级控件应用之九 ServiceController
Stage2_Lesson5Service初步 Stage2_Lesson10应用程序签名及发布 Stage2_Lesson8ContentProvider Stage2_Lesson9BroadcastReceiver Stage2_Lesson6神奇的UI我来了 Stage2_Lesson7Android数据存储 Stage3_Lesson1App...
在Android开发领域,深入理解和掌握高级应用源码是提升技能的关键步骤。"Android高级应用源码-100多个Android实例集合.zip" 提供了一大批实际应用案例,这对于开发者来说是一份宝贵的资源。通过研究这些源码,我们...
Stage2_Lesson5Service初步 Stage2_Lesson10应用程序签名及发布 Stage2_Lesson8ContentProvider Stage2_Lesson9BroadcastReceiver Stage2_Lesson6神奇的UI我来了 Stage2_Lesson7Android数据存储 Stage3_Lesson1App...
Stage2_Lesson5Service初步 Stage2_Lesson10应用程序签名及发布 Stage2_Lesson8ContentProvider Stage2_Lesson9BroadcastReceiver Stage2_Lesson6神奇的UI我来了 Stage2_Lesson7Android数据存储 Stage3_Lesson1App...
Stage2_Lesson5Service初步 Stage2_Lesson10应用程序签名及发布 Stage2_Lesson8ContentProvider Stage2_Lesson9BroadcastReceiver Stage2_Lesson6神奇的UI我来了 Stage2_Lesson7Android数据存储 Stage3_Lesson1App...
【SQL Server 2005 ETL专家系列-05 SQL Server 2005 Integration Service的高级应用】 本课程是SQL Server 2005 ETL专家系列的一部分,主要聚焦于SQL Server 2005 Integration Services (SSIS) 的高级应用。ETL...
在Android开发领域,高级应用的实现往往涉及到对系统API的深入理解和创新性使用。这个"Android高级应用源码-仿快按钮 360智键 米键.zip"压缩包提供了一个独特的学习资源,它旨在模仿流行的硬件快捷按钮,如360智键和...
在这个"Android高级应用源码-隐藏安装包图标使用其他应用启动本应用.zip"的压缩包中,包含两个子文件:openHideIcon.zip和hideIcon.zip,它们分别代表了如何隐藏应用图标以及如何通过其他应用启动隐藏的应用。...