简体   繁体   中英

Android: how to save sensor data continuously while app is doing other works

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) { 
           }
    }
};

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM