java - Save sensors' data in android -


trying save sensors data file don't know problem here. ui freezes after press start button, , want know how write these data xml file instead of text file.

mainactivity

 public class mainactivity extends activity implements sensoreventlistener {      edittext txtdata;     button startbutton;     button stopbutton;      file myfile;     fileoutputstream fout;     outputstreamwriter myoutwriter;     bufferedwriter mybufferedwriter;     printwriter myprintwriter;       private sensormanager sensormanager;     private long currenttime;     private long starttime;      float[] acceleration = new float[3];     float[] rotationrate = new float[3];     float[] magneticfield = new float[3];      boolean stopflag = false;     boolean startflag = false;     boolean isfirstset = true;      @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // file name entered         txtdata = (edittext) findviewbyid(r.id.edittext2);         txtdata.sethint("enter file name here...");          // start button         startbutton = (button) findviewbyid(r.id.button1);         startbutton.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                 // start recording sensor data                 try {                     myfile = new file("/sdcard/" + txtdata.gettext() + ".txt");                     myfile.createnewfile();                      fout = new fileoutputstream(myfile);                     myoutwriter = new outputstreamwriter(fout);                     mybufferedwriter = new bufferedwriter(myoutwriter);                     myprintwriter = new printwriter(mybufferedwriter);                      toast.maketext(getbasecontext(), "start recording data set", toast.length_short).show();                 } catch (exception e) {                     toast.maketext(getbasecontext(), e.getmessage(), toast.length_short).show();                 } {                     startflag = true;                 }             }         });          // stop button         stopbutton = (button) findviewbyid(r.id.button2);         stopbutton.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                 // stop recording sensor data                 try {                     stopflag = true;                     toast.maketext(getbasecontext(), "done recording data set", toast.length_short).show();                 } catch (exception e) {                     toast.maketext(getbasecontext(), e.getmessage(), toast.length_short).show();                 }             }         });          sensormanager = (sensormanager) getsystemservice(sensor_service);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public void onsensorchanged(sensorevent event) {         if (startflag) {              if (event.sensor.gettype() == sensor.type_accelerometer) {                 acceleration[0] = event.values[0];                 acceleration[1] = event.values[1];                 acceleration[2] = event.values[2];             }              if (event.sensor.gettype() == sensor.type_gyroscope) {                 rotationrate[0] = event.values[0];                 rotationrate[1] = event.values[1];                 rotationrate[2] = event.values[2];             }              if (event.sensor.gettype() == sensor.type_magnetic_field) {                 magneticfield[0] = event.values[0];                 magneticfield[1] = event.values[1];                 magneticfield[2] = event.values[2];             }              if (isfirstset) {                 starttime = system.currenttimemillis();                 isfirstset = false;             }              currenttime = system.currenttimemillis();              (int = 0; i<1; i++) {                 if (!stopflag) {                     save();                 }                  else {                     try {                         myoutwriter.close();                     } catch (ioexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     } catch (nullpointerexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         fout.close();                     } catch (ioexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     } catch (nullpointerexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                 }             }         }     }      private void save() {              myprintwriter.write(currenttime - starttime + " " + acceleration[0] + " " + acceleration[1] + " " + acceleration[2]                         + " " + rotationrate[0] + " " + rotationrate[1] + " " + rotationrate[2]                          + " " + magneticfield[0] + " " + magneticfield[1] + " " + magneticfield[2] + "\n");     }      @override     protected void onresume() {         super.onresume();         // register class listener sensors         sensormanager.registerlistener(this, sensormanager.getdefaultsensor(sensor.type_accelerometer), sensormanager.sensor_delay_normal);         sensormanager.registerlistener(this, sensormanager.getdefaultsensor(sensor.type_gyroscope), sensormanager.sensor_delay_normal);         sensormanager.registerlistener(this, sensormanager.getdefaultsensor(sensor.type_magnetic_field), sensormanager.sensor_delay_normal);     }      @override     protected void onpause() {         // unregister listener         super.onpause();         sensormanager.unregisterlistener(this);     }      @override     public void onaccuracychanged(sensor arg0, int arg1) {         // todo auto-generated method stub      } } 

manifest

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.btest"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk         android:minsdkversion="8"         android:targetsdkversion="18" />      <uses-permission android:name="android.permission.write_external_storage" />     <uses-permission android:name="android.permission.write_internal_storage" />     <uses-permission android:name="android.permission.internet"/>     <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.example.btest.mainactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -