单独螺纹中的加速度计传感器 [英] Acclerometer Sensor in Separate Thread

查看:24
本文介绍了单独螺纹中的加速度计传感器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据从加速度计传感器保存到数据库,但我想在单独的线程中进行.我试图在互联网上搜索它,但大多数都使用相同的线程.

I am saving data from Accelerometer Sensor to Database, but I want to do it in a separate thread. I tried to search it on Internet but most of them uses the same thread.

我尝试过的事情:

SenSorEventListener sel;
    Thread A=new Thread(){
                public void run()
                {
                    sel=new SensorEventListener() {

                        @Override
                        public void onSensorChanged(SensorEvent event) {
                            // TODO Auto-generated method stub
                            double Acceleration,x,y,z;
                            x=event.values[0];
                            y=event.values[2];
                            z=event.values[2];
                            Acceleration=Math.sqrt(x*x+y*y+z*z);
                            db.addAccel(Acceleration);
                            Log.d("MESSAGE","SAVED");
                        }

                        @Override
                        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                            // TODO Auto-generated method stub

                        }
                    };
                }
            };
            A.start();
            try {
                A.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sm.registerListener(sel,s,1000000);
}

我拿了一个 SensorEventListener,在一个新线程中初始化它,然后用 register listener 注册它.

I took a SensorEventListener, initialized it in a new Thread and then registering it with register listener.

另一种方法:我使用 Runnable 接口实现了 Accelerometer 类,在构造函数中初始化了所有东西,所以我的 run() 方法是空白的,但这种方法不会创建新的线程.

Another approach: I implemented Accelerometer class using Runnable interface, initialized everything in Constructor, so my run() method is blank, but this approach doesn't create a new Thread.

    Accelerometer(Context con,Database d)
        {   
            sm=(SensorManager)con.getSystemService(Context.SENSOR_SERVICE);
            s=sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
            sm.registerListener(this,s,1000000);
            db=d;
        }
   void run()
   {}

我很乐意尝试另一种方法,或者听到我在上述方法中做错了什么.

I would be happy to try another approach, or to hear I am doing something wrong in the above approaches.

推荐答案

首先你需要使用 registerListener(SensorEventListener listener, Sensor sensor, int rate, Handler handler) 并提供一个 Handler 在后台线程上运行.

First you need to use registerListener(SensorEventListener listener, Sensor sensor, int rate, Handler handler) and supply a Handler that is running on a background thread.

创建一个HandlerThread,获取它的Looper,创建一个提供looper的Handler.

Create a HandlerThread, get its Looper, create a Handler supplying the looper.

这将让您在后台线程上接收回调.完成后确保移除监听器,然后调用 Looper.quit(),退出线程.

This will let you receive the callbacks on the background thread. When finished ensure you remove the listener and then call Looper.quit(), to exit the thread.

这篇关于单独螺纹中的加速度计传感器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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