0
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, 37) 错误: 找不到符号类 SensorEvent 错误:(11, 8) 错误: MainActivity 不是抽象的并且没有覆盖抽象方法 onAccuracyChanged(Sensor ,int) in SensorEventListener 错误:(21, 39) 错误: 包 r 不存在 错误:(42, 5) 错误: 方法没有覆盖或实现超类型中的方法

4

2 回答 2

1

SensorEventListener 有两个抽象方法:

1) onAccuracyChanged(Sensor sensor, int accuracy) 2) onSensorChanged(SensorEvent event)

您还需要覆盖 onAccuracyChanged 方法。

  @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

当传感器的精度发生变化时调用此方法。如果你不想在这里做任何事情,那么不要在这里写任何代码。但是由于它的抽象,您需要实现它。

希望能帮助到你。

于 2017-03-26T09:46:29.743 回答
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);
    }

}
于 2017-03-26T10:08:49.003 回答