티스토리 뷰

Android/Sensor

[ Sensor #1 ] TYPE_STEP_DETECTOR

녹색꼬맹이 2024. 2. 29. 17:46
반응형
SMALL

 

반응형

 

만보기

 

class StepRecordHelper(
    private val callback: () -> Unit
): SensorEventListener {

    private var sensorManager: SensorManager? = null
    private var sensor: Sensor? = null


    fun start(context: Context) {
        sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
        sensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
        sensorManager?.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
    }

    fun stop() {
        sensorManager?.unregisterListener(this)
    }

    override fun onSensorChanged(event: SensorEvent?) {
        if (event?.sensor?.type == Sensor.TYPE_STEP_DETECTOR) {
            if (event.values[0] == 1.0f) {
                callback.invoke()
            }
        }
    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {

    }
}

 

 

private val stepRecordHelper by lazy {
    StepRecordHelper(callback = {
        // todo
    })
}




// 실행
stepRecordHelper.start(this)

// 멈춤
stepRecordHelper.stop()
반응형
LIST
댓글