android之broadcast发送广播

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

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. static final Object mStartingServiceSync = new Object();
  12. static PowerManager.WakeLock mStartingService;

  13. private Context mContext = null;

  14. @SuppressWarnings("deprecation")
  15. @Override
  16. public void onReceive(Context context, Intent intent) {
  17. if(mContext == null) mContext = context;

  18. if(DEBUG){
  19. Log.v(TAG, "====================action=" + intent.getAction());
  20. }

  21. Intent i = new Intent();
  22. i.setClass(mContext, TestUpdateService.class);
  23. i.putExtras(intent);
  24. i.putExtra("action", intent.getAction());
  25. beginStartingService(mContext, i);
  26. }

  27. public static void beginStartingService(Context context, Intent intent) {
  28. synchronized (mStartingServiceSync) {
  29. if (mStartingService == null) {
  30. PowerManager pm =
  31. (PowerManager)context.getSystemService(Context.POWER_SERVICE);
  32. mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  33. "StartingAlertService");
  34. mStartingService.setReferenceCounted(false);
  35. }
  36. mStartingService.acquire();
  37. context.startService(intent);
  38. }
  39. }

  40. /**
  41. * Called back by the service when it has finished processing notifications,
  42. * releasing the wake lock if the service is now stopping.
  43. */
  44. public static void finishStartingService(Service service, int startId) {
  45. synchronized (mStartingServiceSync) {
  46. if (mStartingService != null) {
  47. if (service.stopSelfResult(startId)) {
  48. mStartingService.release();
  49. }
  50. }
  51. }
  52. }

  53. }

同时启动一个服务

  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. @Override
  6. public IBinder onBind(Intent intent) {
  7. // TODO Auto-generated method stub
  8. return null;
  9. }

  10. private volatile Looper mServiceLooper = null;
  11. private volatile Handler mUpdateHandler;

  12. private class UpdateHandler extends Handler{

  13. public UpdateHandler(Looper looper) {
  14. super(looper);
  15. }
  16. @Override
  17. public void handleMessage(Message msg) {
  18. // TODO Auto-generated method stub
  19. processMessage(msg);

  20. TestUpdateReceiver.finishStartingService(TestUpdateService.this, msg.arg1);
  21. }
  22. }

  23. @Override
  24. public void onCreate() {
  25. // TODO Auto-generated method stub
  26. super.onCreate();
  27. mContext = this;

  28. HandlerThread thread = new HandlerThread("TestUpdateService");
  29. thread.start();
  30. mServiceLooper = thread.getLooper();
  31. mUpdateHandler = new UpdateHandler(mServiceLooper);
  32. }

  33. @Override
  34. public int onStartCommand(Intent intent, int flags, int startId) {
  35. // TODO Auto-generated method stub
  36. if(intent != null){
  37. Message msg = mUpdateHandler.obtainMessage();
  38. msg.obj = intent.getExtras();
  39. msg.arg1 = startId;
  40. mUpdateHandler.sendMessage(msg);
  41. }
  42. return START_REDELIVER_INTENT;
  43. }
  44. private void processMessage(Message msg){

  45. Bundle bundle = (Bundle) msg.obj;

  46. String action = bundle.getString("action");
  47. if(DEBUG){
  48. Log.v(TAG, "processMessage(Message msg)=================action=" + action);
  49. }

  50. /*在这里做你要做的事情*/
  51. /*在这里做你要做的事情*/
  52. /*在这里做你要做的事情*/
  53. }

  54. }

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

最后,不要忘了在配置文件里加相应的权限
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值