当前位置:   article > 正文

Android点亮屏幕功能_android 亮屏

android 亮屏

应用在涉及音视频播放功能时可能需要点亮屏幕,此文把点亮屏幕相关工具类和使用类的代码贴出来,若有问题欢迎指正!

工具类:WakeLockUtil,相关代码如下:

  1. public class WakeLockUtil {
  2. /**
  3. * 点亮屏幕
  4. *
  5. * @param timeout The timeout after which to release the wake lock, in milliseconds.
  6. */
  7. @Nullable
  8. public static PowerManager.WakeLock acquireWakeLock(@NonNull Context context, long timeout) {
  9. PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  10. if (pm == null)
  11. return null;
  12. PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
  13. PowerManager.FULL_WAKE_LOCK |
  14. PowerManager.ON_AFTER_RELEASE,
  15. context.getClass().getName());
  16. wakeLock.acquire(timeout);
  17. return wakeLock;
  18. }
  19. public static void release(@Nullable PowerManager.WakeLock wakeLock) {
  20. if (wakeLock != null && wakeLock.isHeld()) {
  21. wakeLock.release();
  22. }
  23. }
  24. }

使用类:WakeLockActivity,相关代码如下:

  1. public class WakeLockActivity extends AppCompatActivity {
  2. private PowerManager.WakeLock mWakeLock;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. //点亮屏幕2分钟,2分钟后此方法会自动释放PowerManager.WakeLock
  8. mWakeLock = WakeLockUtil.acquireWakeLock(this, 2 * 60 * 1000);
  9. }
  10. @Override
  11. protected void onDestroy() {
  12. super.onDestroy();
  13. WakeLockUtil.release(mWakeLock);
  14. }
  15. }

相关权限如下:

<uses-permission android:name="android.permission.WAKE_LOCK" />

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/619605
推荐阅读
相关标签
  

闽ICP备14008679号