Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在“待机”期间保持SensorEventListener_Android_Accelerometer_Android Sensors_Wakelock_Standby - Fatal编程技术网

Android 在“待机”期间保持SensorEventListener

Android 在“待机”期间保持SensorEventListener,android,accelerometer,android-sensors,wakelock,standby,Android,Accelerometer,Android Sensors,Wakelock,Standby,我正在构建一个应用程序,它需要一直收听加速计,即使应用程序不在前台。 在这一点上,我想澄清的是,这不是一个游戏商店的应用程序。这是一款专门用于内部使用的应用程序,电池电量消耗不是什么大问题。此外,如果有人知道其他检查用户是否行走的方法,请告诉我 为了实现这一点,我创建了一个BroadcastReceiver,它监听android.intent.action.BOOT_,并启动一个注册SensorEventListener的服务。该服务获得了一个部分唤醒锁,我希望有了这个锁,SensorEvent

我正在构建一个应用程序,它需要一直收听加速计,即使应用程序不在前台。 在这一点上,我想澄清的是,这不是一个游戏商店的应用程序。这是一款专门用于内部使用的应用程序,电池电量消耗不是什么大问题。此外,如果有人知道其他检查用户是否行走的方法,请告诉我

为了实现这一点,我创建了一个BroadcastReceiver,它监听android.intent.action.BOOT_,并启动一个注册SensorEventListener的服务。该服务获得了一个部分唤醒锁,我希望有了这个锁,SensorEventListener仍能收到onChange回调,但这并没有发生。但是,使用全屏唤醒锁定,它可以工作-但我不想让屏幕一直打开,用户也可以按下待机按钮。。。 在我的调试会话中,我从未看到服务被破坏,因此,不应该释放唤醒锁

以下是获取锁并注册传感器的服务类:

public class WalkingDetectorService extends Service {

    private static PowerManager.WakeLock wakeLock = null;
    private static final String LOCK_TAG = "WALKING_DETECTOR";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        acquireLock(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            // started on boot from a BroadcastReceiver
        startMotionDetector(getApplication());
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        releaseLock();
        super.onDestroy();
    }

    private void startMotionDetector(Application application) {

        int sensitivityIdx = SettingsManager.getInt(application, SettingsManager.SETTING_SENSOR_SENSITIVITY);

        if (sensitivityIdx == -1) {
            sensitivityIdx = MotionSensor.DEFAULT_SENSITITY_IDX;
        }

        float fSens = MotionSensor.getSensivity(sensitivityIdx);

        //SensorEventListener is registered here. It is properly done. Only stops listen when screen goes off (by timeout or user pressing the standby button)
            MotionSensor.startMotionSensor(application, fSens);

    }

    public static synchronized void acquireLock(Context ctx) {
        if (wakeLock == null) {
            PowerManager mgr = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
            wakeLock = mgr.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOCK_TAG);
            wakeLock.setReferenceCounted(true);
        }
        wakeLock.acquire();
    }

    public static synchronized void releaseLock() {
        if (wakeLock != null) {
            if (wakeLock.isHeld())
            {
                wakeLock.release();
            }
        }
    }
}
我想我提供了从你那里得到帮助所需的所有信息。 如果你觉得有一些代码是有用的,我很乐意与你合作!多谢各位