日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android之broadcast发送广播

發(fā)布時間:2025/4/5 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android之broadcast发送广播 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們有時會遇到這樣的情況,當(dāng)手機(jī)處于睡眠狀態(tài)時,到了某個時間點,我們需要做一些必要的事情。這是如何做到的呢?我們首先會想到鬧鐘,設(shè)置一個鬧鐘,到了設(shè)置的時間點,鬧鐘就會響。當(dāng)然,還有很多其他的應(yīng)用...
下面給出一個例子,方便學(xué)習(xí)和查閱

BroadcastReceiver
  • package com.android.test;

  • import android.app.Service;
  • import android.content.BroadcastReceiver;
  • import android.content.Context;
  • import android.content.Intent;
  • import android.os.PowerManager;
  • import android.util.Log;

  • public class TestUpdateReceiver extends BroadcastReceiver{
  • private static final String TAG = "TestUpdateReceiver";
  • private static final boolean DEBUG = true;

  • static final Object mStartingServiceSync = new Object();
  • static PowerManager.WakeLock mStartingService;

  • private Context mContext = null;

  • @SuppressWarnings("deprecation")
  • @Override
  • public void onReceive(Context context, Intent intent) {
  • if(mContext == null) mContext = context;

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

  • Intent i = new Intent();
  • i.setClass(mContext, TestUpdateService.class);
  • i.putExtras(intent);
  • i.putExtra("action", intent.getAction());
  • beginStartingService(mContext, i);
  • }

  • public static void beginStartingService(Context context, Intent intent) {
  • synchronized (mStartingServiceSync) {
  • if (mStartingService == null) {
  • PowerManager pm =
  • (PowerManager)context.getSystemService(Context.POWER_SERVICE);
  • mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  • "StartingAlertService");
  • mStartingService.setReferenceCounted(false);
  • }
  • mStartingService.acquire();
  • context.startService(intent);
  • }
  • }

  • /**
  • * Called back by the service when it has finished processing notifications,
  • * releasing the wake lock if the service is now stopping.
  • */
  • public static void finishStartingService(Service service, int startId) {
  • synchronized (mStartingServiceSync) {
  • if (mStartingService != null) {
  • if (service.stopSelfResult(startId)) {
  • mStartingService.release();
  • }
  • }
  • }
  • }

  • }

  • 同時啟動一個服務(wù)

  • package com.android.test;

  • public class TestUpdateService extends Service{
  • private static final String TAG = "TestUpdateService";
  • private static final boolean DEBUG = true;

  • @Override
  • public IBinder onBind(Intent intent) {
  • // TODO Auto-generated method stub
  • return null;
  • }

  • private volatile Looper mServiceLooper = null;
  • private volatile Handler mUpdateHandler;

  • private class UpdateHandler extends Handler{

  • public UpdateHandler(Looper looper) {
  • super(looper);
  • }
  • @Override
  • public void handleMessage(Message msg) {
  • // TODO Auto-generated method stub
  • processMessage(msg);

  • TestUpdateReceiver.finishStartingService(TestUpdateService.this, msg.arg1);
  • }
  • }

  • @Override
  • public void onCreate() {
  • // TODO Auto-generated method stub
  • super.onCreate();
  • mContext = this;

  • HandlerThread thread = new HandlerThread("TestUpdateService");
  • thread.start();
  • mServiceLooper = thread.getLooper();
  • mUpdateHandler = new UpdateHandler(mServiceLooper);
  • }

  • @Override
  • public int onStartCommand(Intent intent, int flags, int startId) {
  • // TODO Auto-generated method stub
  • if(intent != null){
  • Message msg = mUpdateHandler.obtainMessage();
  • msg.obj = intent.getExtras();
  • msg.arg1 = startId;
  • mUpdateHandler.sendMessage(msg);
  • }
  • return START_REDELIVER_INTENT;
  • }
  • private void processMessage(Message msg){

  • Bundle bundle = (Bundle) msg.obj;

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

  • /*在這里做你要做的事情*/
  • /*在這里做你要做的事情*/
  • /*在這里做你要做的事情*/
  • }

  • }

  • 原理是先設(shè)一個鬧鐘,等待鬧鐘發(fā)送廣播,TestUpdateReceiver接收該廣播,并獲得 PowerManager.WakeLock的一個實例mStartingService ,并執(zhí)行 mStartingService.acquire(),然后去做你想做的事情,最后執(zhí)行mStartingService . release ( ) 把獲得的lock釋放掉。關(guān)于電源管理的知識,請查閱其他資料。

    最后,不要忘了在配置文件里加相應(yīng)的權(quán)限

    總結(jié)

    以上是生活随笔為你收集整理的android之broadcast发送广播的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。