Android accelerometer not working when screen is turned off

前端 未结 6 1636
[愿得一人]
[愿得一人] 2020-11-29 17:38

I\'m developing an application for my final thesis on computer science, and I need to collect and log accelerometer data. I need to acquire it for a whole day long, so there

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 18:28

    I applied the PARTIAL_WAKE_LOCK and it worked like charm to me. Tested on OS 5.0, 6.0 & 7.0. Here's my code to acquire and release wake lock. Be careful it increased the battery drainage, so acquire and release it intelligently.

    public void acquireWakeLock() {
        final PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        releaseWakeLock();
        //Acquire new wake lock
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
        mWakeLock.acquire();
    }
    
    public void releaseWakeLock() {
        if (mWakeLock != null && mWakeLock.isHeld()) {
            mWakeLock.release();
            mWakeLock = null;
        }
    }
    

    Ref: https://developer.android.com/training/scheduling/wakelock.html#cpu

    I am working on a prediction sdk, who use device sensors (accelerometer/gyroscope) data and predicts the user events. I experienced the same issue.

提交回复
热议问题