-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
60 lines (48 loc) · 1.63 KB
/
MainActivity.java
File metadata and controls
60 lines (48 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.example.owner.stepcounter;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager;
private TextView txt_steps;
boolean running = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_steps = (TextView) findViewById(R.id.txt_steps);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
running = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null){
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
}
else {
Toast.makeText(this, "Step sensor not available!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
super.onPause();
running = false;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (running){
txt_steps.setText(String.valueOf(event.values[0]));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}