Android 状态锁

原创
2022/05/22 11:22
阅读数 97

(1)常见的锁

WifiLock

Allows an application to keep the Wi-Fi radio awake.Normally the Wi-Fi radio may turn off when the
user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock
 is released. Multiple applications may hold WifiLocks, and the radio will only be allowed to turn 
off when no WifiLocks are held in any application.

允许应用程序保持Wi-Fi唤醒状态。通常情况下, Wi-Fi网络可以关闭,当用户没有使用该设备在一段时间。获取WifiLock将保持
网络,直到锁被释放。多个应用程序可持有WifiLocks ,而收音机将只能被允许关闭时,没有WifiLocks在任何应用程序举行。
WifiManager.WifiLock使用场景:通过WifiLock来锁定wifi网络,使其一直保持连接,直到这个锁定被释放。如果app需要下载
很大的文件,就需要保持wifi锁,来确保app有足够的时间下载完成。wifi锁不能超越wifi-enabled设置,也无法阻止飞行模式。

WakeLock

用于唤醒屏幕、CPU、键盘

Flag Value

CPU

Screen

Keyboard

PARTIAL_WAKE_LOCK

On*

Off

Off

SCREEN_DIM_WAKE_LOCK

On

Dim (暗淡)

Off

SCREEN_BRIGHT_WAKE_LOCK

On

Bright

Off

FULL_WAKE_LOCK

On

Bright

Bright

 

具体使用时,都需要权限

android.Manifest.permission.WAKE_LOCK

(2)具体实现参考Exo

/* package */ final class WakeLockManager {

  private static final String TAG = "WakeLockManager";
  private static final String WAKE_LOCK_TAG = "ExoPlayer:WakeLockManager";

  @Nullable private final PowerManager powerManager;
  @Nullable private WakeLock wakeLock;
  private boolean enabled;
  private boolean stayAwake;

  public WakeLockManager(Context context) {
    powerManager =
        (PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
  }

  /**
   * Sets whether to enable the acquiring and releasing of the {@link WakeLock}.
   *
   * <p>By default, wake lock handling is not enabled. Enabling this will acquire the wake lock if
   * necessary. Disabling this will release the wake lock if it is held.
   *
   * <p>Enabling {@link WakeLock} requires the {@link android.Manifest.permission#WAKE_LOCK}.
   *
   * @param enabled True if the player should handle a {@link WakeLock}, false otherwise.
   */
  public void setEnabled(boolean enabled) {
    if (enabled) {
      if (wakeLock == null) {
        if (powerManager == null) {
          Log.w(TAG, "PowerManager is null, therefore not creating the WakeLock.");
          return;
        }
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
        wakeLock.setReferenceCounted(false);
      }
    }

    this.enabled = enabled;
    updateWakeLock();
  }

  /**
   * Sets whether to acquire or release the {@link WakeLock}.
   *
   * <p>Please note this method requires wake lock handling to be enabled through setEnabled(boolean
   * enable) to actually have an impact on the {@link WakeLock}.
   *
   * @param stayAwake True if the player should acquire the {@link WakeLock}. False if the player
   *     should release.
   */
  public void setStayAwake(boolean stayAwake) {
    this.stayAwake = stayAwake;
    updateWakeLock();
  }

  // WakelockTimeout suppressed because the time the wake lock is needed for is unknown (could be
  // listening to radio with screen off for multiple hours), therefore we can not determine a
  // reasonable timeout that would not affect the user.
  @SuppressLint("WakelockTimeout")
  private void updateWakeLock() {
    if (wakeLock == null) {
      return;
    }

    if (enabled && stayAwake) {
      wakeLock.acquire();
    } else {
      wakeLock.release();
    }
  }
}
final class WifiLockManager {

  private static final String TAG = "WifiLockManager";
  private static final String WIFI_LOCK_TAG = "ExoPlayer:WifiLockManager";

  @Nullable private final WifiManager wifiManager;
  @Nullable private WifiLock wifiLock;
  private boolean enabled;
  private boolean stayAwake;

  public WifiLockManager(Context context) {
    wifiManager =
        (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  }

  /**
   * Sets whether to enable the usage of a {@link WifiLock}.
   *
   * <p>By default, wifi lock handling is not enabled. Enabling will acquire the wifi lock if
   * necessary. Disabling will release the wifi lock if held.
   *
   * <p>Enabling {@link WifiLock} requires the {@link android.Manifest.permission#WAKE_LOCK}.
   *
   * @param enabled True if the player should handle a {@link WifiLock}.
   */
  public void setEnabled(boolean enabled) {
    if (enabled && wifiLock == null) {
      if (wifiManager == null) {
        Log.w(TAG, "WifiManager is null, therefore not creating the WifiLock.");
        return;
      }
      wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, WIFI_LOCK_TAG);
      wifiLock.setReferenceCounted(false);
    }

    this.enabled = enabled;
    updateWifiLock();
  }

  /**
   * Sets whether to acquire or release the {@link WifiLock}.
   *
   * <p>The wifi lock will not be acquired unless handling has been enabled through {@link
   * #setEnabled(boolean)}.
   *
   * @param stayAwake True if the player should acquire the {@link WifiLock}. False if it should
   *     release.
   */
  public void setStayAwake(boolean stayAwake) {
    this.stayAwake = stayAwake;
    updateWifiLock();
  }

  private void updateWifiLock() {
    if (wifiLock == null) {
      return;
    }

    if (enabled && stayAwake) {
      wifiLock.acquire();
    } else {
      wifiLock.release();
    }
  }
}

 

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部