package com.example.ritwik.letswalk;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
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);
}
@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, "Sensor Not Found",Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause()
{
super.onPause();
running=false;
}
@Override
protected void onSensorChangeed(SensorEvent event)
{
if(running)
{
tv_steps.setText(String.valueOf(event.values[0]));
}
}
}
代码有以下错误
错误:(21,15)错误:找不到符号类TextView 错误:任务':app:compileDebugJavaWithJavac'的执行失败。 编译失败;请参阅编译器错误输出以获取详细信错误:(12,5)错误:找不到符号类TextView错误:(43,47) 错误:找不到符号类SensorEvent错误:(11,8)错误: MainActivity不是抽象的,不会覆盖抽象方法 SensorEventListener中的onAccuracyChanged(Sensor,int)错误:(21,39) 错误:包r不存在错误:(42,5)错误:方法没有 从超类型
覆盖或实现方法
答案 0 :(得分:1)
SensorEventListener有两个抽象方法:
1)onAccuracyChanged(传感器传感器,int精度) 2)onSensorChanged(SensorEvent事件)
您还需要覆盖onAccuracyChanged方法。
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
当传感器的精度发生变化时,会调用此方法。如果你不想在这里做任何事情,那么不要在这里写任何代码。但是,因为它的摘要你需要实现它。
希望它有所帮助。
答案 1 :(得分:0)
在我的计步器应用程序中我做了
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float[] values = event.values;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
textView.setText("" + value);
}
}
public void onAccuracyChanged(Sensor sensor,int a){}
如果你想要整个代码:这里是:
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class CounterActivity extends Activity implements SensorEventListener {
private TextView textView;
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
int value = -1; //Step Counter
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_activity);
textView = (TextView) findViewById(R.id.steps);
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
public void onAccuracyChanged(Sensor sensor,int a){}
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float[] values = event.values;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
textView.setText("" + value);
}
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
}