Created
April 29, 2018 19:01
-
-
Save MadinaB/d90532c0b1a193995a805a472a9f24e8 to your computer and use it in GitHub Desktop.
StepCounter for Android KitKat
This file contains 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
MainActivity.java | |
package com.madinabektayeva.stepcounter; | |
import android.content.Context; | |
import android.hardware.*; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import static android.hardware.SensorManager.SENSOR_DELAY_UI; | |
public class MainActivity extends AppCompatActivity implements SensorEventListener { | |
TextView tv_steps; | |
SensorManager sensorManager; | |
boolean running = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
tv_steps = (TextView) findViewById(R.id.tv_steps); | |
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
} | |
protected void onResume(){ | |
super.onResume(); | |
running = true; | |
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); | |
if(countSensor!= null){ | |
sensorManager.registerListener((SensorEventListener) this, countSensor, SensorManager.SENSOR_DELAY_UI); | |
}else{ | |
Toast.makeText(this, "Sensor not found", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
protected void onPause(){ | |
super.onPause(); | |
sensorManager.unregisterListener((SensorListener) this); | |
running = false; | |
} | |
public void onSensorChanged(SensorEvent event){ | |
if(running){ | |
tv_steps.setText(""+String.valueOf(event.values[0])); | |
} | |
} | |
public void onAccuracyChanged(Sensor sensor, int accuracy){ | |
if(running){ | |
} | |
} | |
} | |
AndroidManifest.xml | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.madinabektayeva.stepcounter"> | |
<uses-feature android:name="android.hardware.sensor.stepcounter" android:required="true" /> | |
<uses-feature android:name="android.hardware.sensor.stepdetector" android:required="true" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> | |
activity_main.xml | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
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" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:paddingBottom="16dp" | |
android:paddingTop="16dp" | |
android:paddingLeft="16dp" | |
android:paddingRight="16dp" | |
tools:context="com.madinabektayeva.stepcounter.MainActivity"> | |
<TextView | |
android:id="@+id/tv_info" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Steps" /> | |
<TextView | |
android:id="@+id/tv_steps" | |
android:layout_marginTop="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="0" | |
android:textSize="40dp"/> | |
</LinearLayout> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4343050/