zoukankan      html  css  js  c++  java
  • wakelock

    •  PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 Context.getSystemService().方法获取PowerManager实例。
    • 然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
    • 获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。

    关于int flags

    各种锁的类型对CPU 、屏幕、键盘的影响:

    PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。

    SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯

    SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯

    FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度

    ACQUIRE_CAUSES_WAKEUP:Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

    ON_AFTER_RELEASE:f this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

    权限获取

    要进行电源的操作需要在AndroidManifest.xml中声明该应用有设置电源管理的权限。

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    你可能还需要
    <uses-permission android:name="android.permission.DEVICE_POWER" />

    另外WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的。

    可以在activity的onResume方法里面操作WakeLock,  在onPause方法里面释放。

    e.g:

    //每场游戏开始时都要进行的初始化
    if (wakeLock == null) {
    Log.v("acquire=wakelock ", "acquire");
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getCanonicalName());
    wakeLock.acquire();
    }



    @Override
    protected void onDestroy() {
    GameCenter.getInstance().sendInGameCmd(GameCenter.USER_QUIT_TYPE, null, null);
    if (wakeLock != null && wakeLock.isHeld()) {
    Log.v("acquire=wakelock ", "release");
    wakeLock.release();
    wakeLock = null;
    }
    super.onDestroy();
    EventBus.getDefault().unregister(this);//反注册EventBus
    }
  • 相关阅读:
    对象遍历 for in ,数组遍历for in 与 for of 的区别
    计算一个数组中key值相同的数量
    VUE的两种跳转push和replace对比区别
    微信公众号二次分享ios分享失败问题
    获得对象中的键或值
    第一个table根据checkbox选择tr,在另一个table中显示对应索引的tr(jq遍历的运用)
    checkbox 全选反选 获得所有的checkbox
    为什么jQuery要return this.each()?
    用jq代码写出一个轮播图。
    页面滚动到一定位置,两个div 朝中间运动。
  • 原文地址:https://www.cnblogs.com/visuals/p/5194561.html
Copyright © 2011-2022 走看看