Chinaunix首页 | 论坛 | 博客
  • 博客访问: 285930
  • 博文数量: 68
  • 博客积分: 1474
  • 博客等级: 上尉
  • 技术积分: 616
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-12 12:07
文章分类

全部博文(68)

文章存档

2011年(68)

分类: 嵌入式

2011-10-06 16:31:47

我们有时会遇到这样的情况,当手机处于睡眠状态时,到了某个时间点,我们需要做一些必要的事情。这是如何做到的呢?我们首先会想到闹钟,设置一个闹钟,到了设置的时间点,闹钟就会响。当然,还有很多其他的应用...
下面给出一个例子,方便学习和查阅

BroadcastReceiver
  1. package com.android.test;

  2. import android.app.Service;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.PowerManager;
  7. import android.util.Log;

  8. public class TestUpdateReceiver extends BroadcastReceiver{
  9.     private static final String TAG = "TestUpdateReceiver";
  10.     private static final boolean DEBUG = true;
  11.     
  12.     static final Object mStartingServiceSync = new Object();
  13.     static PowerManager.WakeLock mStartingService;
  14.     
  15.     private Context mContext = null;
  16.     
  17.     @SuppressWarnings("deprecation")
  18.     @Override
  19.     public void onReceive(Context context, Intent intent) {
  20.         if(mContext == null) mContext = context;
  21.         
  22.         if(DEBUG){
  23.             Log.v(TAG, "====================action=" + intent.getAction());
  24.         }
  25.             
  26.         Intent i = new Intent();
  27.         i.setClass(mContext, TestUpdateService.class);
  28.         i.putExtras(intent);
  29.         i.putExtra("action", intent.getAction());
  30.         beginStartingService(mContext, i);
  31.     }
  32.     
  33.     public static void beginStartingService(Context context, Intent intent) {
  34.         synchronized (mStartingServiceSync) {
  35.             if (mStartingService == null) {
  36.                 PowerManager pm =
  37.                     (PowerManager)context.getSystemService(Context.POWER_SERVICE);
  38.                 mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  39.                         "StartingAlertService");
  40.                 mStartingService.setReferenceCounted(false);
  41.             }
  42.             mStartingService.acquire();
  43.             context.startService(intent);
  44.         }
  45.     }

  46.     /**
  47.      * Called back by the service when it has finished processing notifications,
  48.      * releasing the wake lock if the service is now stopping.
  49.      */
  50.     public static void finishStartingService(Service service, int startId) {
  51.         synchronized (mStartingServiceSync) {
  52.             if (mStartingService != null) {
  53.                 if (service.stopSelfResult(startId)) {
  54.                     mStartingService.release();
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     
  60. }

同时启动一个服务

  1. package com.android.test;

  2. public class TestUpdateService extends Service{
  3.     private static final String TAG = "TestUpdateService";
  4.     private static final boolean DEBUG = true;
  5.     
  6.     @Override
  7.     public IBinder onBind(Intent intent) {
  8.         // TODO Auto-generated method stub
  9.         return null;
  10.     }

  11.     private volatile Looper mServiceLooper = null;
  12.     private volatile Handler mUpdateHandler;
  13.     
  14.     private class UpdateHandler extends Handler{
  15.         
  16.         public UpdateHandler(Looper looper) {
  17.             super(looper);
  18.         }
  19.         @Override
  20.         public void handleMessage(Message msg) {
  21.             // TODO Auto-generated method stub
  22.             processMessage(msg);
  23.             
  24.             TestUpdateReceiver.finishStartingService(TestUpdateService.this, msg.arg1);
  25.         }
  26.     }
  27.     
  28.     @Override
  29.     public void onCreate() {
  30.         // TODO Auto-generated method stub
  31.         super.onCreate();
  32.         mContext = this;
  33.         
  34.         HandlerThread thread = new HandlerThread("TestUpdateService");
  35.         thread.start();
  36.         mServiceLooper = thread.getLooper();
  37.         mUpdateHandler = new UpdateHandler(mServiceLooper);
  38.     }

  39.     @Override
  40.     public int onStartCommand(Intent intent, int flags, int startId) {
  41.         // TODO Auto-generated method stub
  42.         if(intent != null){
  43.             Message msg = mUpdateHandler.obtainMessage();
  44.             msg.obj = intent.getExtras();
  45.             msg.arg1 = startId;
  46.             mUpdateHandler.sendMessage(msg);
  47.         }
  48.         return START_REDELIVER_INTENT;
  49.     }
  50.   
  51.     private void processMessage(Message msg){
  52.         
  53.         Bundle bundle = (Bundle) msg.obj;
  54.         
  55.         String action = bundle.getString("action");
  56.         if(DEBUG){
  57.             Log.v(TAG, "processMessage(Message msg)=================action=" + action);
  58.         }

  59.         /*在这里做你要做的事情*/
  60.         /*在这里做你要做的事情*/
  61.         /*在这里做你要做的事情*/
  62.         
  63.     }

  64. }

原理是先设一个闹钟,等待闹钟发送广播,TestUpdateReceiver接收该广播,并获得PowerManager.WakeLock的一个实例mStartingService,并执行 mStartingService.acquire(),然后去做你想做的事情,最后执行mStartingService.release()把获得的lock释放掉。关于电源管理的知识,请查阅其他资料。

最后,不要忘了在配置文件里加相应的权限
阅读(4274) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~