Accl​​erometer传感器在单独的线程线程、传感器、Accl、erometer

2023-09-05 00:14:27 作者:再見小時候

我从加速计传感器将数据保存到数据库中,但我想这样做在一个单独的线程。 我试图寻找它在互联网上,但他们大多使用相同的线程。 请帮忙。 我已经尝试过的事情: 1)

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. Please help. Things i have Tried: 1)

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,在一个新的线程进行初始化,然后用寄存器监听器注册了。

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

另一种方法: 我实现Accelerometer类采用Runnable接口,在构造函数初始化的一切,所以我的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()
   {}

请给我建议的任何其它方式或者如果我做错了什么在上面的方法。

Please suggest me any another approach or if i am doing something wrong in the above approaches.

推荐答案

首先,您需要使用 registerListener(SensorEventListener监听器,感应式传感器,INT率,处理程序处理)和提供一个处理程序是在后台线程上运行。

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 ,得到了尺蠖,创建一个处理程序供应弯针。

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.