Stop recording accelerometer data with button clic

2019-08-08 01:11发布

问题:

I have written the following lines to record accelerometer data in a file with a button click

startButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {


            final SensorEventListener mySensorEventListener = new SensorEventListener() { 
                public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {


                xAxis_lateralA = sensorEvent.values[0]; 
                yAxis_longitudinalA = sensorEvent.values[1]; 
                zAxis_verticalA = sensorEvent.values[2]; // TODO apply the acceleration changes to your application.
                textView.append("\nACC_x = "+ xAxis_lateralA + ", ACC_y = "+yAxis_longitudinalA+ ", ACC_z = " + zAxis_verticalA);
                acc+="\n"+xAxis_lateralA + ", "+ yAxis_longitudinalA+", "+zAxis_verticalA;

                try {
                    File myFile = new File("/sdcard/acc.txt");
                    myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);
                    OutputStreamWriter myOutWriter = 
                                            new OutputStreamWriter(fOut);
                    myOutWriter.append(acc);
                    myOutWriter.close();
                    fOut.close();
                    Toast.makeText(getBaseContext(),
                            "Done writing SD 'acc.txt'",
                            Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }

                }
            }
                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                    // TODO Auto-generated method stub

                }
            };
            // write on SD card file data in the text box
            sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
            int sensorType = Sensor.TYPE_ACCELEROMETER; 
            sm.registerListener(mySensorEventListener,sm.getDefaultSensor(sensorType), SensorManager.SENSOR_DELAY_NORMAL);

        }// onClick
        });

Now I want it to stop recording the data with another button click. For example -

stopButton.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {
        int sensorType = Sensor.TYPE_ACCELEROMETER; 
        sm.unregisterListener(listener, sensor)
     }// onClick
  }); // btnstopButton
}

Wanted to use unregisterListener but most of the time it is saying deprecated.

Can anyone help please?

回答1:

I think you want to declare your SensorEventListener object outside of the onClick() method so you can unregister it in the other onClick() method:

sm.unregisterListener(mySensorEventListener, sensorType);

That's the method signature you want to use according to the docs.



回答2:

The documentation says that using the methods associated with SensorListener are deprecated, and to use SensorEventListener instead. Your code snippet should not throw a deprecated warning, but if you use SensorListener, it would.