I have the following code which gives a beep when the (mobile) device is nudged slightly:
let audio = new Audio('ack.mp3'); function handleMotionEvent(event) { let x = event.accelerationIncludingGravity.x; let y = event.accelerationIncludingGravity.y; if (Math.abs(x) + Math.abs(y) > 2.2) { audio.play(); } } window.addEventListener("devicemotion", handleMotionEvent, true); It works fine, but not at all when the device is locked. Is there any way I can detect this while the device is locked?
1 Answers
Answers 1
Seems you have to acquire a partial wake lock for that operation with. PowerManager class.
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead"); lock.acquire(); //and then window.addEventListener("devicemotion", handleMotionEvent, true); You need also this permission in the AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK" />
0 comments:
Post a Comment