Android SensorManager Sensor,kotlin

该文章展示了一段Android应用程序代码,主要用Kotlin实现SensorManager来获取设备的传感器信息。通过注册SensorEventListener监听器,对SensorEvent进行处理,例如打印传感器名称。文章还提及了如何注册自定义监听器接口MyListener,以及在Activity销毁时释放资源。此外,提到了一系列关于不同传感器如压力传感器、步进计数器、接近传感器、加速度传感器和光照传感器的博客文章链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android SensorManager Sensor,kotlin

import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    companion object {
        private const val TAG: String = "fly"
    }

    private var sm: SensorManager? = null
    private var sensor: Sensor? = null
    private var listeners: ArrayList<MyListener> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sm = this.getSystemService(SENSOR_SERVICE) as SensorManager?
        sensor = sm?.getDefaultSensor(Sensor.TYPE_ALL)
        sm?.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        for (i in 0 until 5) {
            register(object : MyListener {
                override fun onChange(event: SensorEvent) {
                    Log.d(TAG, event.sensor.name)
                }
            })
        }
    }

    private val listener: SensorEventListener = object : SensorEventListener {
        override fun onSensorChanged(event: SensorEvent) {
            change(event)
        }

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

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        sm?.unregisterListener(listener)

        listeners.clear()
    }

    fun change(event: SensorEvent) {
        for (l in listeners) {
            l.onChange(event)
        }
    }

    fun register(l: MyListener) {
        listeners.add(l)
    }

    interface MyListener {
        fun onChange(event: SensorEvent) {
            Log.d(TAG, event.toString())
        }
    }
}

 

 

 

 

 

Android获得当前设备支持的所有传感器_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorManager;import android.support.v7.app.AppCompatActivity;import android.os.Bunhttps://blog.csdn.net/zhangphil/article/details/78105016

Android Pressure Sensor_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.Senhttps://blog.csdn.net/zhangphil/article/details/78109536

Android Step Counter & Detector Sensor_android stepcounter_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.Senhttps://blog.csdn.net/zhangphil/article/details/78136188

Android Proximity Sensor近距离传感器P-Sensor_proximity 传感器_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.Senhttps://blog.csdn.net/zhangphil/article/details/78095467

Android Accelerometer Sensor_vue android,sensor.accelerometer_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.Senhttps://blog.csdn.net/zhangphil/article/details/78091706

Android Orientation Sensor_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.Senhttps://blog.csdn.net/zhangphil/article/details/78119760

Android Light Sensor_zhangphil的博客-CSDN博客package zhangphil.sensor;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.Senhttps://blog.csdn.net/zhangphil/article/details/78110512

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值