机器人传感器registerListener在单独的线程 [英] Android sensor registerListener in a separate thread

查看:532
本文介绍了机器人传感器registerListener在单独的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在后台记录加速度计值的服务。我们的想法是要注册为传感器侦听和值写入存储器。我已经启用了 StrictMode.enableDefaults() MainActivity 应用程序(不在服务)的检查该服务在自己的线程中运行。但事实并非如此!我不知道是什么问题...我有,我希望以照顾的问题本的AsyncTask 类,但它显然没有(这是内部服务其执行方法是从服务的的onCreate 方法调用。

I have wrote a service to log accelerometer values in background. The idea is to register for sensor listener and write the values to storage. I have enabled the StrictMode.enableDefaults() in the MainActivity of the app (not in the service) to check if the service runs on its own thread. But it does not! I don't know what is the problem...I have this AsyncTask class that I hoped to take care of the problem but it apparently did not (this is inside the service and its execute method is called from the onCreate method of the service.

private class LogTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {

        mListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {

                String formatted = String.valueOf(System.currentTimeMillis())
                        + "\t" + String.valueOf(sensorEvent.timestamp)
                        + "\t" + String.valueOf(sensorEvent.values[0])
                        + "\t" + String.valueOf(sensorEvent.values[1])
                        + "\t" + String.valueOf(sensorEvent.values[2])
                        + "\r\n";

                //if (mIsServiceStarted && mFileStream != null && mLogFile.exists()) {
                if (mFileStream != null && mLogFile.exists()) {

                    try {
                        mFileStream.write(formatted.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };

        mSensorManager.registerListener(
                mListener, mSensor, SensorManager.SENSOR_DELAY_FASTEST
        );

        return null;
    }
}

StrictMode日志猫特意点了以下行排尿政策:

StrictMode in log cat specially points that the following line is voiding the policy:

mFileStream.write(formatted.getBytes());

StrictMode policy violation; ~duration=0 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=31 violation=1
            at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1092)

谁能告诉我是怎么回事了?这里是我服务的全code如果requried:
http://pastebin.com/NRm376BL

推荐答案

我发现的问题是,我应该创建一个 HandlerThread 处理程序和处理程序传递给 registerListener 方法的重载。如果你不提供这个说法听众会由主线程来处理大部分的这个时候是不是你在找什么。

I found out that the problem is, I should create a HandlerThread and a Handler and pass the handler to an overload of registerListener method. If you dont provide this argument the listener will be handled by the main thread and most of the time this isn't what you are looking for.

有关示例实现检查该code:特别是对由主线程在另一个线程处理传感器听众不是

For an example implementation check this code: particularly for handling sensor listener on another thread not by main thread

http://pastebin.com/QuHd0LNU

这篇关于机器人传感器registerListener在单独的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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