Chinaunix首页 | 论坛 | 博客
  • 博客访问: 113244
  • 博文数量: 23
  • 博客积分: 471
  • 博客等级: 一等列兵
  • 技术积分: 251
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-21 15:21
文章分类
文章存档

2017年(1)

2013年(2)

2011年(20)

分类: Android平台

2013-04-12 14:33:41



Services初步

有编程经验的朋友一听名字service就应该知道是提供的某种服务,而且是后台长时运行的,不提供ui,同样这也是android提供的又一个重要的应用组件。

service的两种表现形式

Started

应用组件(如activity)调用startService(),service被启动,一旦启动,通常,service在后台一直运行,不会和应用有任何交互,操作完成后,service应该自己停止。

Bound

应用组件(如activity)调用bindService(),service被绑定,一旦绑定,通常,service提供了允许组件和service有数据交互的客户端-服务器接口,只有组件绑定了service的情况下,service才会运行。

注意:service是运行在主线程的,如果有耗时操作,需要开线程来做。很方便的是android提供了一些类来帮助大家使用,比如下面介绍的IntentService(不能处理并发任务)

继承IntentService

需要实现的接口


构造函数:需要调用父类IntentService(string) 参数是队列名
onHandleIntent(Intent intent)

所做的工作:

1. 创建一个默认线程,此工作线程传递intent到onStartCommand,这样和主线程分离。
2. 创建一个工作队列,一次传递一个intent到onHandleIntent(),不用担心多线程并发问题。
3. 当所有请求被处理完毕,会自动停止service,不用手动调用stopSelf
4.提供默认onBind()实现
5.提供默认onStartCommand(),发送intent到工作队列,再到onHandleIntent()。


继承Service



点击(此处)折叠或打开

  1. public class HelloService extends Service {
  2.   private Looper mServiceLooper;
  3.   private ServiceHandler mServiceHandler;

  4.   // Handler that receives messages from the thread
  5.   private final class ServiceHandler extends Handler {
  6.       public ServiceHandler(Looper looper) {
  7.           super(looper);
  8.       }
  9.       @Override
  10.       public void handleMessage(Message msg) {
  11.           // Normally we would do some work here, like download a file.
  12.           // For our sample, we just sleep for 5 seconds.
  13.           long endTime = System.currentTimeMillis() + 5*1000;
  14.           while (System.currentTimeMillis() < endTime) {
  15.               synchronized (this) {
  16.                   try {
  17.                       wait(endTime - System.currentTimeMillis());
  18.                   } catch (Exception e) {
  19.                   }
  20.               }
  21.           }
  22.           // Stop the service using the startId, so that we don't stop
  23.           // the service in the middle of handling another job
  24.           stopSelf(msg.arg1);
  25.       }
  26.   }

  27.   @Override
  28.   public void onCreate() {
  29.     // Start up the thread running the service. Note that we create a
  30.     // separate thread because the service normally runs in the process's
  31.     // main thread, which we don't want to block. We also make it
  32.     // background priority so CPU-intensive work will not disrupt our UI.
  33.     HandlerThread thread = new HandlerThread("ServiceStartArguments",
  34.             Process.THREAD_PRIORITY_BACKGROUND);
  35.     thread.start();
  36.     
  37.     // Get the HandlerThread's Looper and use it for our Handler
  38.     mServiceLooper = thread.getLooper();
  39.     mServiceHandler = new ServiceHandler(mServiceLooper);
  40.   }

  41.   @Override
  42.   public int onStartCommand(Intent intent, int flags, int startId) {
  43.       Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

  44.       // For each start request, send a message to start a job and deliver the
  45.       // start ID so we know which request we're stopping when we finish the job
  46.       Message msg = mServiceHandler.obtainMessage();
  47.       msg.arg1 = startId;
  48.       mServiceHandler.sendMessage(msg);
  49.       
  50.       // If we get killed, after returning from here, restart
  51.       return START_STICKY;
  52.   }

  53.   @Override
  54.   public IBinder onBind(Intent intent) {
  55.       // We don't provide binding, so return null
  56.       return null;
  57.   }
  58.   
  59.   @Override
  60.   public void onDestroy() {
  61.     Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
  62.   }
  63. }

start a service

Intent intent = new Intent(this, HelloService.class); startService(intent);
startService(intent);


阅读(1922) | 评论(0) | 转发(0) |
0

上一篇:Activity初步

下一篇:【必然】-凯文凯利

给主人留下些什么吧!~~