Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击按钮停止记录加速计数据_Android_Accelerometer - Fatal编程技术网

Android 单击按钮停止记录加速计数据

Android 单击按钮停止记录加速计数据,android,accelerometer,Android,Accelerometer,我已经写了以下几行,通过点击按钮将加速度计数据记录到文件中 startButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { final SensorEventListener mySensorEventListener = new SensorEventListener() { public void onSen

我已经写了以下几行,通过点击按钮将加速度计数据记录到文件中

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
        });
现在,我希望它停止记录另一个按钮点击数据。例如—

stopButton.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {
        int sensorType = Sensor.TYPE_ACCELEROMETER; 
        sm.unregisterListener(listener, sensor)
     }// onClick
  }); // btnstopButton
}
想要使用unregisterListener,但大多数情况下,它表示已弃用


有人能帮忙吗?

说不推荐使用与SensorListener关联的方法,而是使用SensorEventListener。您的代码段不应抛出不推荐的警告,但如果您使用SensorListener,则会抛出警告。

说明使用与SensorListener关联的方法是不推荐的,请改用SensorEventListener。您的代码段不应抛出不推荐使用的警告,但如果您使用SensorListener,它会抛出警告。

我认为您希望在onClick方法之外声明SensorEventListener对象,以便在其他onClick方法中注销它:

sm.unregisterListener(mySensorEventListener, sensorType);

这是您希望根据使用的方法签名。

我认为您希望在onClick方法之外声明SensorEventListener对象,以便在其他onClick方法中注销它:

sm.unregisterListener(mySensorEventListener, sensorType);
这是您想要使用的方法签名,根据