Created
July 14, 2020 17:56
-
-
Save peacher5/593caf2e29a8c6b2c7a3adc79ede4eb5 to your computer and use it in GitHub Desktop.
My Step Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <TextView | |
| android:id="@+id/stepTextView" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Hello World!" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintLeft_toLeftOf="parent" | |
| app:layout_constraintRight_toRightOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> | |
| </androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package my.iampeach.mystepcounter | |
| import android.content.Context | |
| import android.hardware.Sensor | |
| import android.hardware.SensorEvent | |
| import android.hardware.SensorEventListener | |
| import android.hardware.SensorManager | |
| import androidx.appcompat.app.AppCompatActivity | |
| import android.os.Bundle | |
| import android.widget.Toast | |
| import kotlinx.android.synthetic.main.activity_main.* | |
| class MainActivity : AppCompatActivity(), SensorEventListener { | |
| var sensorManager: SensorManager? = null | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager | |
| } | |
| override fun onResume() { | |
| super.onResume() | |
| val stepsSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) | |
| if (stepsSensor == null) { | |
| Toast.makeText(this, "No Step Counter Sensor !", Toast.LENGTH_SHORT).show() | |
| } else { | |
| sensorManager?.registerListener(this, stepsSensor, SensorManager.SENSOR_DELAY_UI) | |
| } | |
| } | |
| override fun onPause() { | |
| super.onPause() | |
| sensorManager?.unregisterListener(this) | |
| } | |
| override fun onAccuracyChanged(p0: Sensor?, p1: Int) { | |
| } | |
| override fun onSensorChanged(event: SensorEvent) { | |
| stepTextView.text = "" + event.values[0] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment