免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1586 | 回复: 0
打印 上一主题 下一主题

android-BroadcastReceiver后台处理广播消息 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 01:27 |只看该作者 |倒序浏览
我们有时会遇到这样的情况,当手机处于睡眠状态时,到了某个时间点,我们需要做一些必要的事情。这是如何做到的呢?我们首先会想到闹钟,设置一个闹钟,到了设置的时间点,闹钟就会响。当然,还有很多其他的应用...
下面给出一个例子,方便学习和查阅

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释放掉。关于电源管理的知识,请查阅其他资料。

最后,不要忘了在配置文件里加相应的权限
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP