Code:
package com.yotamarker.testgyro
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() , AccelerometerListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override protected fun onResume() {
super.onResume()
if (AccelerometerManager.isSupported(this))
{
AccelerometerManager.startListening(this)
}
}
override fun onAccelerationChanged(x:Float, y:Float, z:Float) {
tv1.setText("x: = " + x + " y= " + y + " z = " + z)
}
override fun onShake(force:Float) {
Toast.makeText(this, "Motion detected", Toast.LENGTH_SHORT).show()
}
override fun onStop() {
super.onStop()
//Check device supported Accelerometer senssor or not
if (AccelerometerManager.isListening())
{
//Start Accelerometer Listening
AccelerometerManager.stopListening()
Toast.makeText(this, "onStop Accelerometer Stopped", Toast.LENGTH_SHORT).show()
}
}
override fun onDestroy() {
super.onDestroy()
if (AccelerometerManager.isListening())
{
AccelerometerManager.stopListening()
Toast.makeText(this, "onDestroy Accelerometer Stopped", Toast.LENGTH_SHORT).show()
}
}
}
annotation class overrides