如何在AccessibilityService内连续读取加速计传感器值5秒?

时间:2015-09-08 11:51:53

标签: android accelerometer android-sensors

我正在使用AccessibilityService来监控前景活动何时发生变化。此外,每当发生这样的变化时,我需要读取接下来5秒的加速度计读数。我实现了以下代码,但我没有获得连续读数。有时2个app开关之间没有显示读数

我能够准确地检测前景活动变化,并且我可以独立运行一个可以显示传感器状态变化的活动。但结合两者存在问题。

public class WindowChangeDetectingService extends AccessibilityService implements SensorEventListener {

    @Override
    protected void onServiceConnected() {
            ...
    }
    SensorManager mSensorManager;
    Sensor mAccelerometerSensor;
    SensorEventListener mListener;
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {

            try {
                mSensorManager.unregisterListener(mListener);
            }
            catch (Exception e){
                Log.d("not unregs",e.toString());
            }

            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);

            Handler h = new Handler();
            h.postDelayed(new Runnable() {

                @Override
                public void run() {
                    //    do stuff with sensor values

                    mSensorManager.unregisterListener(mListener);
                }
            }, 5000);

            ComponentName componentName = new ComponentName(
                    event.getPackageName().toString(),
                    event.getClassName().toString()
            );

            ActivityInfo activityInfo = tryGetActivity(componentName);
            boolean isActivity = activityInfo != null;
            if (isActivity)
                Log.i("CurrentActivity", componentName.flattenToShortString());
        }
    }
    @Override
    public void onInterrupt() {}

    @Override
    public void onSensorChanged(SensorEvent event) {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
       Log.i("\nAccelerometer: \n","X:"+x+"\tY:"+y+"\tZ:"+z);
    }
}

1 个答案:

答案 0 :(得分:1)

使用curl_setopt($curl, CURLOPT_CAINFO, "cacert.pem"); curl_setopt($curl, CURLOPT_CAPATH, "."); SENSOR_DELAY_GAME代替SENSOR_DELAY_FASTEST

SENSOR_DELAY_NORMAL