Is it possible to detect motion when screen is off?

后端 未结 4 410
情深已故
情深已故 2021-01-13 02:24

My app simply need to detect any motion of the device while screen is off.

I know that accelerometer is used for this task but it don\'t work while screen is off in

4条回答
  •  广开言路
    2021-01-13 03:15

    Partial Wake Lock is all you need to access accelerometer readings while the screen is off.

    You can use it like this:

    private PowerManager.WakeLock mWakeLock;
    
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
    mWakeLock.acquire();
    

    And after you're done, just release the lock:

    mWakeLock.release();
    

    If you obtain accelerometer data in a Service, you could simply acquire lock in it's onCreate() and release in onDestroy().

提交回复
热议问题