安卓:如何不断保存传感器数据,而应用程序是做其他作品 [英] Android: how to save sensor data continuously while app is doing other works

查看:202
本文介绍了安卓:如何不断保存传感器数据,而应用程序是做其他作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯新的到Android。我正在deveping一个应用程序,不断挽救传感器的数据和旁边拍摄相片。其实我要开始节约加速度数据文件的,而拍照,或当屏幕被染黑了。
我已经达到了这一点,当我触摸一个按钮,我的应用程序可以节省传感器数据,但它一次。
我怎样才能使其连续存储数据,并不会影响我的应用程序的其他部分运行?
我想,当屏幕被染黑了,而我在考虑我的应用程序,它的照片保存数据,而我不喜欢的应用程序(例如应用程序被暂停)

PLZ HLP日Thnx。

这是我的code

 私人OnClickListener saveFun =新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){
        // TODO自动生成方法存根
        尝试{
               文件根= Environment.getExternalStorageDirectory();
               如果(root.canWrite()){
                   文件f =新的文件(根的test.txt);
                   FileWriter的FW;
                   如果(saveBtnCnt == 0){
                       FW =新的FileWriter(F);
                       saveBtnCnt = 1;                   }
                   其他{
                       FW =新的FileWriter(F,真);
                   }
                   BufferedWriter将出=新的BufferedWriter(FW);                   C = Calendar.getInstance();
                   毫秒= c.get(Calendar.MILLISECOND);
                   秒= c.get(Calendar.SECOND);
                   分钟= c.get(Calendar.MINUTE);
                   小时= c.get(Calendar.HOUR);
                   天= c.get(Calendar.DAY_OF_MONTH);
                   月= c.get(的Calendar.MONTH)+1;
                   年= c.get(Calendar.YEAR);                   out.write(年+/+月+/+天
                           +\\ n+小时+:+分钟+:,+秒+毫秒+\\ n);
                   out.write(currentX.getText()+\\ n);
                   out.write(currentY.getText()+\\ n);
                   out.write(currentZ.getText()+\\ n);
                   out.close();                   Toast.makeText(getBaseContext(),文件保存成功!
                Toast.LENGTH_SHORT).show();
               }
           }赶上(IOException异常五){
           }
    }
};


解决方案

1)创建服务。

2)具有服务监听传感器事件。

3)在服务上,创建一个线程。

4)在线程中,创建一个消息循环

5)当你到达传感器事件,并将它们发送到线程的消息循环。

6)有线程等待传入事件。当它看到的,有它附加到文件。

7)当服务被停止,取消该线程。当线程被取消,有它关闭了该文件。

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.

这篇关于安卓:如何不断保存传感器数据,而应用程序是做其他作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆