我有一个服务(SkService)用于通过监控ACCELEROMETER从待机状态唤醒我的Nexus4(运行Android 4.2.2),但是当手机进入待机2秒以上时它不起作用。它效果很好我在以下两个案例中: 按下电源键后电话很快就会震动。 2.手机连接到我的笔记本电脑
我不知道为什么。感谢是否有人可以帮助我! 这里的关键代码是:
public class SkService extends Service {
private ShakeDetector mShakeDetector;
@Override
public void onCreate() {
mShakeDetector = new ShakeDetector(SkService.this);
mShakeDetector.setOnShakeListener(new onShakeListener() {
@Override
public void onShake(String value) {
Log.d(TAG, "Get onShake event with value:" + value);
Locker.acquireCpuWakeLock(SkService.this);
Locker.releaseWakeLock();
}
});
mShakeDetector.registerListener();
}
@Override
public void onDestroy() {
Log.d(TAG, "SkService on Destroy");
mShakeDetector.unRegisterListener();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
储物柜:
public class Locker {
private static PowerManager.WakeLock sCpuWakeLock;
public static void acquireCpuWakeLock(Context context) {
Log.d(TAG, "Acquiring cpu wake lock");
if (sCpuWakeLock != null) {
return;
}
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("SKUnlock KeyGuard");
Log.d(TAG, "Disable keyguard...");
kl.disableKeyguard();
Log.d(TAG, "Keyguard disabled and unlock the screen...");
sCpuWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "SKUnlock");
sCpuWakeLock.acquire();
Log.d(TAG, "WakeLock acqiured done");
}
public static void releaseWakeLock() {
if (sCpuWakeLock == null)
return;
sCpuWakeLock.release();
sCpuWakeLock = null;
Log.d(TAG, "WakeLock released");
}
}
我在工作时得到以下日志:
04-04 20:23:34.013 D/com.skunlock(31730): Get onShake event with value:Shook 0 times:
04-04 20:23:34.013 D/com.skunlock(31730): diffTime=64,speed=1588.15. x=2.79, y=-3.25,z=15.27,dX=4.52,dY=4.21,Dirc=Left to Right,accuracy2
04-04 20:23:34.013 D/com.skunlock(31730): Acquiring cpu wake lock
04-04 20:23:34.013 D/com.skunlock(31730): Disable keyguard...
04-04 20:23:34.013 D/com.skunlock(31730): Keyguard disabled and unlock the screen...
04-04 20:23:34.013 I/PowerManagerService( 525): Waking up from sleep...
04-04 20:23:34.023 D/com.skunlock(31730): WakeLock acqiured done
04-04 20:23:34.023 D/com.skunlock(31730): WakeLock released
04-04 20:23:34.023 I/WindowManager( 525): No lock screen!
04-04 20:23:34.043 D/SurfaceFlinger( 158): Screen acquired, type=0 flinger=0x41634318
04-04 20:23:34.043 D/hwcomposer( 158): hwc_blank: Doing Dpy=0, blank=0
04-04 20:23:34.123 D/hwcomposer( 158): hwc_blank: Done Dpy=0, blank=0
04-04 20:23:34.143 D/overlay ( 158): Set pipe=RGB0 dpy=0; Set pipe=RGB1 dpy=0; Set pipe=VG0 dpy=0; Set pipe=VG1 dpy=0;
但是当我的应用程序不能正常工作时,我只有我的应用程序打印的日志,没有任何系统日志(比如,没有锁定屏幕):
04-04 20:23:34.333 D/com.skunlock(31730): Get onShake event with value:Shook 0 times:
04-04 20:23:34.333 D/com.skunlock(31730): diffTime=64,speed=2482.65. x=-1.73, y=-0.91,z=2.05,dX=2.22,dY=7.01,Dirc=Up to Down,accuracy2
04-04 20:23:34.333 D/com.skunlock(31730): Acquiring cpu wake lock
04-04 20:23:34.333 D/com.skunlock(31730): Disable keyguard...
04-04 20:23:34.333 D/com.skunlock(31730): Keyguard disabled and unlock the screen...
04-04 20:23:34.333 D/com.skunlock(31730): WakeLock acqiured done
04-04 20:23:34.333 D/com.skunlock(31730): WakeLock released
04-04 20:23:34.524 D/com.skunlock(31730): Get onShake event with value:Shook 0 times:
答案 0 :(得分:0)
考虑使用WakefulBroadcastReceiver,它会为您创建和管理部分唤醒锁。
您仍然需要使用Manifest上的权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />