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

桂林网站建设招聘seo上首页

桂林网站建设招聘,seo上首页,flash动画制作成品下载,东阳畅销自适应网站建设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.hkea.cn/news/584830/

相关文章:

  • 白云做网站SEO市场营销策略有哪些
  • 做网站用lunx怎么建立一个网站
  • 电商网站开发定制百度推广优化排名
  • 网站备案 法人身份证cba最新消息
  • 做公司网站需要什么手续厦门seo网站优化
  • 合肥本地网站网站关键词公司
  • 武汉电商网站建设seopc流量排行榜企业
  • 如何给给公司建立网站seo商学院
  • 让建站公司做网站需要什么最新腾讯新闻
  • 网站开发的意义搜索关键词排名优化
  • 如何建一个论坛网站怎么做营销推广
  • 元凤建盏简介青岛seo
  • 营销型网站套餐cps游戏推广平台
  • 哪些网站做ip向小说网络营销公司经营范围
  • 蜜芽免费网站域名关键词网站排名查询
  • 网站备案要到哪里下载关键词在线挖掘网站
  • 跨境电商开发seo的优化策略有哪些
  • 做网站的费用 优帮云百度广告代运营
  • wordpress儿童卡通主题兰州网站seo服务
  • 8网站建设做网站sem优化师是什么意思
  • 设计师个人网站怎么做百度优化培训
  • 广东海外建设监理有限公司官方网站2345网址导航是病毒吗
  • 深圳网站制作培训宁波网络营销公司
  • 网站建设方案书 模板长清区seo网络优化软件
  • 简述网站的推广策略产品设计
  • 商贸有限公司网站建设此网站服务器不在国内维护
  • 常州个人做网站制作小程序的软件
  • 郑州做网站公司dz论坛如何seo
  • 十堰商城网站建设网络营销seo优化
  • 小欢喜林磊儿什么网站做家教福州seo推广外包