android – 从广播接收器获取唤醒锁定的问题

前端之家收集整理的这篇文章主要介绍了android – 从广播接收器获取唤醒锁定的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有个问题.我正在尝试让广播接收器获得唤醒锁定,以便我的闹钟将手机从睡眠模式唤醒.

在下面的广播接收器中,程序在“scpuWakeLock.acquire()”行上遇到“source not found”崩溃;当AlarmReceiver调用类“AlarmAlertWakeLock”时.
知道发生了什么事吗?有没有更好的方法来做我想做的事情?@H_404_4@

在一个文件中:@H_404_4@

  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4.  
  5. public class AlarmReceiver extends BroadcastReceiver {
  6. @Override
  7. public void onReceive(final Context context,Intent intent) {
  8. AlarmAlertWakeLock.acquirecpuWakeLock(context);
  9.  
  10. }
  11. }

在单独的文件中:@H_404_4@

  1. import android.content.Context;
  2. import android.os.PowerManager;
  3.  
  4. public class AlarmAlertWakeLock {
  5.  
  6. private static PowerManager.WakeLock scpuWakeLock;
  7.  
  8. static void acquirecpuWakeLock(Context context) {
  9.  
  10. if (scpuWakeLock != null) {
  11. return;
  12. }
  13. PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  14.  
  15.  
  16. scpuWakeLock = pm.newWakeLock(
  17. PowerManager.PARTIAL_WAKE_LOCK |
  18. PowerManager.ACQUIRE_CAUSES_WAKEUP,"okTag");
  19. scpuWakeLock.acquire();
  20. }
  21.  
  22. static void releasecpuLock() {
  23. if (scpuWakeLock != null) {
  24. scpuWakeLock.release();
  25. scpuWakeLock = null;
  26. }
  27. }
  28. }

解决方法

没关系,我想出来了 – 我需要为清单添加唤醒锁定权限:

uses-permission android:name =“android.permission.WAKE_LOCK”@H_404_4@

现在工作正常!@H_404_4@

猜你在找的Android相关文章