java如何检测设备是否有传感器使用Kotlin中的指南针?
有些设备没有内置传感器来使用指南针
如果手机不支持此功能,我尝试在这里导航或向用户显示消息
我正在尝试:
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)
if (sensor !== null) {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}else{
val intent = Intent(this, SetProfileActivity::class.java)
startActivity(intent)
}
但每次都会出现此错误:
sensorManager.getDefault…(Sensor.TYPE_ORIENTATION) must not be null
我做错了什么
# 1 楼答案
我只是被同样的问题绊倒了
这是我找到的解决方案,对我很有效: