1

im new to android. i'm deveping an app to continuously save sensor's data and beside that takes photos. in fact i want to start saving accelerometer data in to a file while taking photos, or while screen is blacked out. i have reached to this point that my app saves sensor data as i touch a button, but it does it once. how can i make it to saves data continuously and without affecting other parts of my app to run? and i want it saves data while i'm taking photos in my app, when screen is blacked out, while i'm out of my app ( for example app is paused)

plz hlp thnx.

here is my code

private OnClickListener saveFun =new OnClickListener(){

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        try { 
               File root = Environment.getExternalStorageDirectory(); 
               if (root.canWrite()){ 
                   File f = new File(root, "test.txt"); 
                   FileWriter fw;
                   if ( saveBtnCnt == 0){
                       fw = new FileWriter(f);
                       saveBtnCnt = 1;

                   }
                   else{
                       fw = new FileWriter(f,true); 
                   }
                   BufferedWriter out = new BufferedWriter(fw); 

                   c = Calendar.getInstance();
                   miliseconds = c.get(Calendar.MILLISECOND);
                   seconds = c.get(Calendar.SECOND);
                   minutes = c.get(Calendar.MINUTE);
                   hour = c.get(Calendar.HOUR);
                   day = c.get(Calendar.DAY_OF_MONTH);
                   month = c.get(Calendar.MONTH)+1;
                   year = c.get(Calendar.YEAR);

                   out.write(year+"/"+month+"/"+day
                           +"\n"+hour+":"+minutes+":"+seconds+"."+miliseconds+"\n");
                   out.write(currentX.getText()+"\n");
                   out.write(currentY.getText()+"\n");
                   out.write(currentZ.getText()+"\n");
                   out.close(); 

                   Toast.makeText(getBaseContext(), "File saved successfully!",
                Toast.LENGTH_SHORT).show();
               } 
           } catch (IOException e) { 
           }
    }
};
green-dev
  • 103
  • 1
  • 1
  • 8
  • 1
    Aside: you should really find out how to use a `SimpleDateFormat` instead of using `year+"/"+month+"/"+day+"\n"+hour+":"+minutes+":"+seconds+"."+miliseconds+"\n"`. – Andy Turner Jan 25 '16 at 22:37

1 Answers1

2

1)Create a service.

2)Have the service listen for sensor events.

3)In the service, create a thread.

4)In the thread, create a message loop

5)When you get sensor events, send them to the thread's message loop.

6)Have the thread wait for an incoming event. When it sees one, have it append to the file.

7)When the service is stopped, cancel the thread. When the thread is canceled, have it close out the file.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Hi, I have a similar app: a service which listens to accelerometer sensor, on each onChange I save the data to 1024 byte buffer, when it's full append it to file, but I'm doesn't using thread. Why is it necessary? Everything seems to work fine instead of the battery consumption - any change this is related to the missing thread? TNX!! – sharonooo Jan 27 '16 at 16:26
  • Issuing a thread allows you to offload the file io off the ui thread and be more responsive. It isn't completely necessary, but can be a good idea, depending on how much work you're doing. – Gabe Sechan Jan 27 '16 at 18:30