提问人:Vamsi Krishna M S 提问时间:6/5/2023 最后编辑:Vamsi Krishna M S 更新时间:6/5/2023 访问量:22
如何为加速度计传感器读数添加 TImeStamp?
How to add a TImeStamp for the Accelerometer Sensor Reading?
问:
我使用 kotlin 在我的 android 应用程序中创建了一个传感器读数。但是我想实现一个 TimeStamp 来注意传感器中每个事件更改的时间。 谁能帮我?
类 MainActivity : AppCompatActivity(),SensorEventListener { private lateinit var mSensorManager: SensorManager private var mAccelerometer : 传感器 ?=null 私有 var resume = false private lateinit var square: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
}
override fun onSensorChanged(event: SensorEvent?) {
if(event != null && resume){
if(event?.sensor?.type == Sensor.TYPE_ACCELEROMETER){
val sides = event.values[0]
val upDown = event.values[1]
square = findViewById(R.id.sensor_value)
square.apply{
rotationX = upDown * 3f
rotationY = sides * 3f
rotation = -sides
translationX = sides * -10
translationY = upDown * 10
/*
// set reference times
if (sensorTimeReference === 0L && myTimeReference === 0L) {
sensorTimeReference = event.timestamp
myTimeReference = System.currentTimeMillis()
}
// set event timestamp to current time in milliseconds
event.timestamp = myTimeReference +
Math.round((event.timestamp - sensorTimeReference) / 1000000.0)
// some code... */
}
val color = if(upDown.toInt() == 0 && sides.toInt() == 0) Color.GREEN else Color.WHITE
square.setBackgroundColor(color)
square.text = "Accelerometer readings \n\n up/down ${upDown.toInt()}\nleft/right ${sides.toInt()} \n "
}
}
}
override fun onResume() {
super.onResume()
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST,
SensorManager.SENSOR_DELAY_FASTEST)
}
override fun onPause(){
super.onPause()
mSensorManager.unregisterListener(this)
}
fun resumeReading(view: View){
this.resume = true
}
fun pauseReading(view: View){
this.resume = false
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
return
}
override fun onDestroy(){
mSensorManager.unregisterListener(this)
super.onDestroy()
}
}
答: 暂无答案
评论