当前位置: 首页 > news >正文

重庆平台网站建设哪家有西安网站搭建公司

重庆平台网站建设哪家有,西安网站搭建公司,邢台八方网络科技有限公司,wordpress淘宝客类网站建设WakefulBroadcastReceiver 是一种特殊类型的广播接收器,为应用创建和管理 PARTIAL_WAKE_LOCK 。 简单来说, WakefulBroadcastReceiver 是持有系统唤醒锁的 BroadcastReceiver ,用于执行需要保持CPU运转的场景。 注册 注册 Receiver &#…

WakefulBroadcastReceiver 是一种特殊类型的广播接收器,为应用创建和管理 PARTIAL_WAKE_LOCK 。

简单来说,
WakefulBroadcastReceiver 是持有系统唤醒锁的 BroadcastReceiver ,用于执行需要保持CPU运转的场景。

注册

注册 Receiver ,

<receiver android:name=".MyWakefulReceiver"></receiver>

实现MyWakefulReceiver

重写 onReceive() 方法,使用 startWakefulService(Context context, Intent intent) 启动 Service ,

    public class MyWakefulReceiver extends WakefulBroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// Start the service, keeping the device awake while the service is// launching. This is the Intent to deliver to the service.Intent service = new Intent(context, MyIntentService.class);startWakefulService(context, service);}}

Service

Service 里执行正常的逻辑,实行结束后调用 MyWakefulReceiver.completeWakefulIntent(Intent intent) 方法。

public class MyIntentService extends IntentService {public MyIntentService() {super("MyIntentService");}@Overrideprotected void onHandleIntent(Intent intent) {Bundle extras = intent.getExtras();// Do the work that requires your app to keep the CPU running.// ...// Release the wake lock provided by the WakefulBroadcastReceiver.MyWakefulReceiver.completeWakefulIntent(intent);}
}

源码分析

startWakefulService(Context context, Intent intent) 方法中,通过 PowerManager.WakeLock 持有了系统锁,并为每个 intent 设置了一个 id 用于标识,存储在 sActiveWakeLocks 数组中,

/*** Do a {@link android.content.Context#startService(android.content.Intent)* Context.startService}, but holding a wake lock while the service starts.* This will modify the Intent to hold an extra identifying the wake lock;* when the service receives it in {@link android.app.Service#onStartCommand* Service.onStartCommand}, it should pass back the Intent it receives there to* {@link #completeWakefulIntent(android.content.Intent)} in order to release* the wake lock.** @param context The Context in which it operate.* @param intent The Intent with which to start the service, as per* {@link android.content.Context#startService(android.content.Intent)* Context.startService}.*/public static ComponentName startWakefulService(Context context, Intent intent) {synchronized (sActiveWakeLocks) {int id = mNextId;mNextId++;if (mNextId <= 0) {mNextId = 1;}intent.putExtra(EXTRA_WAKE_LOCK_ID, id);ComponentName comp = context.startService(intent);if (comp == null) {return null;}PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"androidx.core:wake:" + comp.flattenToShortString());wl.setReferenceCounted(false);wl.acquire(60 * 1000);sActiveWakeLocks.put(id, wl);return comp;}}

当 Service 调用 MyWakefulReceiver.completeWakefulIntent(Intent intent) 方法时,释放系统锁,并根据 id 将此锁从数组中移除,

/*** Finish the execution from a previous {@link #startWakefulService}.  Any wake lock* that was being held will now be released.** @param intent The Intent as originally generated by {@link #startWakefulService}.* @return Returns true if the intent is associated with a wake lock that is* now released; returns false if there was no wake lock specified for it.*/public static boolean completeWakefulIntent(Intent intent) {final int id = intent.getIntExtra(EXTRA_WAKE_LOCK_ID, 0);if (id == 0) {return false;}synchronized (sActiveWakeLocks) {PowerManager.WakeLock wl = sActiveWakeLocks.get(id);if (wl != null) {wl.release();sActiveWakeLocks.remove(id);return true;}// We return true whether or not we actually found the wake lock// the return code is defined to indicate whether the Intent contained// an identifier for a wake lock that it was supposed to match.// We just log a warning here if there is no wake lock found, which could// happen for example if this function is called twice on the same// intent or the process is killed and restarted before processing the intent.Log.w("WakefulBroadcastReceiv.", "No active wake lock id #" + id);return true;}}
http://www.khdw.cn/news/59592.html

相关文章:

  • 网站优化检测工具枣庄网站建设制作
  • 自己做淘宝客登录网站东莞网站建设工作
  • 创一家网站网上在哪里打广告最有效
  • 要怎么做网站百度收录提交申请网站
  • java jsp 如何做门户网站网页免费制作网站
  • 网站制作软件下载seo优化点击软件
  • 济南网站建设就选搜点网络ok即时热榜
  • 怎样做 云知梦 网站企业邮箱登录入口
  • 施工企业营业额和营业收入区别seo的作用主要有
  • 东莞市人民医院搜索优化软件
  • 妹子ui wordpress百度搜索seo
  • 只使用html做简单网站网站建设技术托管
  • 网站设计一年费用seo推广优化公司哪家好
  • 网站后台灰色无代码网站开发平台
  • b站推广网站入口2024的推广形式深圳网络营销推广外包
  • 开发网站商城站长工具备案查询
  • 可以为自己的小说建设网站网络游戏排行榜百度风云榜
  • 从零开始做网站今天重大新闻头条新闻军事
  • 网络营销外包服务商沈阳seo顾问
  • 做网站如何语音网络销售是做什么的
  • 快飞建站日照网络推广公司
  • 网站设计在营销中的作用网站推广的策略
  • 龙岩任做网站的哪几个比较好千锋教育培训机构就业率
  • 形象墙设计广州网站排名优化报价
  • 宁夏网站开发公司营销网站建设网站开发
  • 新手搭建网站教程朋友圈产品推广文案
  • 中国室内装修设计网温州云优化seo
  • b2c2b网站有哪些免费做网站网站的软件
  • 网站标题 关键字怎么设置代码百度关键词点击
  • 网站在线客服软件seo网络优化专员是什么意思