Android步检测器在一个分离的类中

时间:2015-08-30 07:15:07

标签: android

我正在制作一个有3个班级的应用程序。主要的Activity,一个用于绘制的画布类和一个步进检测器类。我遇到的问题是我的步检测器类检测到我的步骤但我不能在活动主类的另一个类中使用它。我想制作一个标志来检测一个步骤,但它是不可能的。步进检测器:

public class Pasos  implements   SensorEventListener {
    private SensorManager mSensorManager;
    public boolean deteccionPaso;
    private Sensor mStepCounterSensor;
    private Sensor mStepDetectorSensor;

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public Pasos(){
        mSensorManager = (SensorManager) Config.context.getSystemService(Context.SENSOR_SERVICE);
        mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        deteccionPaso=false;
    }


    public boolean isDeteccionPaso() {
        return deteccionPaso;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        float[] values = event.values;
        int value = -1;

        if (values.length > 0) {
            value = (int) values[0];
        }

        if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
            deteccionPaso = true;

            Log.d("paso", "onSensorChanged ");


           // textView.setText("Step Counter Detected : " + value);
        }
        // For test only. Only allowed value is 1.0 i.e. for step taken
        //textView.setText("Step Detector Detected : " + value);
        if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {

         //   deteccionPaso = true;

        }
    }

    public void register(){
        mSensorManager.registerListener(this, mStepCounterSensor, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

   public void unregister(){
        mSensorManager.unregisterListener(this);
   }


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


public class MainActivity extends AppCompatActivity {
    private CanvasView customCanvas;
    public   Pasos pasos;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       customCanvas = (CanvasView) findViewById(R.id.signature_canvas);
       Config.context = this;
       pasos = new Pasos();

       if (pasos.isDeteccionPaso()){
           Log.d("paso", "paso ");
       }
    }

    public void clearCanvas(View v){
        customCanvas.clearCanvas();
    }

    @Override
    protected void onResume(){
        super.onResume();
        pasos.register();
   }

   @Override
   protected void onPause(){
       super.onPause();
       pasos.unregister();
   }

}

0 个答案:

没有答案