- 浏览: 46516 次
- 性别:
- 来自: 西安
最新评论
下面是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 1428Android Intent and Intent ... -
Intent详解
2011-03-03 14:18 1116在应用中,我们可以以两种形式来使用Intent: 直接I ... -
Intent间传送数据一般有两种常用方法
2011-01-24 14:50 1269Intent间传送数据一般有两种常用的办法: 1.extra ... -
android RelativeLayout 详解
2011-01-01 09:46 1561<?xml version="1.0" ... -
How to Use Android Downloads Provider
2010-12-20 21:41 1308本文转载自 http://blog.lytsing.or ... -
android 开发中的一些小知识点
2010-12-17 09:49 9641 关于onPause(): onPause ... -
content provider 深入解析
2010-12-15 22:04 1067Content providers are on ... -
TabActivity
2010-12-12 19:09 1733下面是一个使用android tabactivity 的例子 ... -
android 测试初探(android test)
2010-12-07 15:00 7986android Testing and Instrumenta ... -
Localization of android
2010-12-05 21:51 0android 会在不同地域的不同机器上运行。为了是应用能够 ... -
android高效编程之使用本地变量
2010-12-05 14:26 948hava a look at the following co ... -
在不同的Activity中传递对象的方法
2010-12-02 10:20 1981下面我们将要实现的功 ... -
Cursor与Adapter
2010-11-21 08:34 1203来自: http://hi.baidu.com/lfcaoli ... -
Working with Context Menus
2010-11-15 23:36 828... -
Handler的理解
2010-11-14 20:58 801A Handler allows you to sen ... -
android开发问题解决日志
2010-11-14 18:33 18461.、android中R文件消失的处理方式 ... -
android 中对SDCard 的操作
2010-11-11 14:14 1600对于像视频这样的大文件,我们可以把它存放在SDCard。 SD ... -
ViewAnimation
2010-11-05 12:46 1012view animation 的实现 Understandi ... -
android高效的编写高效代码
2010-10-17 19:13 729本文来源于: 从此学习网 原文: http://www.con ... -
scale animation
2010-10-11 23:29 1585Scale animation: You use this t ...
相关推荐
Android中Service组件的一些高级应用,具体查看博客:http://www.cnblogs.com/plokmju/p/Android_Service2.html
Android高级应用源码-app更新,实现service下载.zip
SQL_Server_2005_Reporting_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...
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 視屏學習資料
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...
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...
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...
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...