how to write sensor data into file android studio

Solutions on MaxInterview for how to write sensor data into file android studio by the best coders in the world

showing results for - "how to write sensor data into file android studio"
Isabel
06 Feb 2016
1public class Main extends Activity implements SensorEventListener {
2
3private SensorManager mSensorManager;
4private Sensor mAccelerometer;
5private FileWriter writer;
6
7/** Called when the activity is first created. */
8@Override
9public void onCreate(Bundle savedInstanceState) {
10    super.onCreate(savedInstanceState);
11    setContentView(R.layout.activiy_main);
12    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
13    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
14}
15
16public void onStartClick(View view) {
17    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
18}
19
20public void onStopClick(View view) {
21    mSensorManager.unregisterListener(this);
22}
23protected void onResume() {
24    super.onResume();
25    writer = new FileWriter("myfile.txt",true);
26}
27
28protected void onPause() {
29    super.onPause();
30
31    if(writer != null) {
32       writer.close();
33    }
34}
35
36@Override
37public void onAccuracyChanged(Sensor sensor, int accuracy) {
38
39}
40
41@Override
42public void onSensorChanged(SensorEvent event) {
43
44    float x = event.values[0];
45    float y = event.values[1];
46    float z = event.values[2];
47    writer.write(x+","+y+","+z+"\n");
48
49 }
50}