如何以编程方式设置Android Sensors的延迟?

时间:2014-02-17 13:09:15

标签: android android-sensors sensormanager

如果有任何方法可以使用常量改变传感器的延迟,例如“Normal_Delay,...”。

我想把延迟值放在我喜欢的位置。如果可能的话?

实际上,我通过使用“处理程序”找到了解决方案,但它不起作用。

以下是使用Handler的解决方案,但它不起作用:

public void registerSensorListener() {
    String sensorName = PackageManager.FEATURE_SENSOR_ACCELEROMETER;
    if (mHandlerThread == null) {
        mHandlerThread = new HandlerThread("sensorThread");
    }
    if (!mHandlerThread.isAlive()) {

        if (SensorUtil.isDeviceHasSensor(sensorName,
                activity.getApplication())) {
            mHandlerThread.start();
            handler = new Handler(mHandlerThread.getLooper());
            mSensorManager.registerListener(this, mAccelerometer, 1000000,
                    handler);
            eventHandler.isDeviceHasSensor(true);
        } else {
            Log.d("Device dos't have Sensor ",
                    PackageManager.FEATURE_SENSOR_ACCELEROMETER);
            eventHandler.isDeviceHasSensor(false);
        }
    } else {
        Log.d("Your Sensor is registered", sensorName);
    }

}

并像这样创建Handler

HandlerThread mHandlerThread; 处理程序处理程序;

mHandlerThread = new HandlerThread(“sensorThread”);

1 个答案:

答案 0 :(得分:3)

最后,我创建了一个解决这个问题的解决方案,通过使用This的澄清现在你可以为你的安卓传感器设置自定义延迟来完善它。!

按如下方式实施:

package com.example.android.location;

public class Sensor extends Activity implements SensorEventListener {
Handler handler;
int interval= 1000; // read sensor data each 1000 ms
boolean flag = false;
boolean isHandlerLive = false;
private Sensor mGravity;
private SensorManager mSensorManager;

private final Runnable processSensors = new Runnable() {
    @Override
    public void run() {
        // Do work with the sensor values.

        flag = true;
        // The Runnable is posted to run again here:
        handler.postDelayed(this, interval);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // you can initialize this wherever you want...
    handler = new Handler();
    mSensorManager = (SensorManager) getApplication().getSystemService(
            Context.SENSOR_SERVICE);
    mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
}

@Override
public void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mGravity,
            SensorManager.SENSOR_DELAY_NORMAL);

    handler.post(processSensors);
}

@Override
public void onPause() {
    handler.removeCallbacks(processSensors);

    super.onPause();
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (flag) {
        // TODO Auto-generated method stub
        // Update UI for one reading every one second...
        Log.d("Sensor Data of X", event.values[0] + "");
        Log.d("Sensor Data of Y", event.values[1] + "");
        Log.d("Sensor Data of Z", event.values[2] + "");

        //
        flag = false;
    }

}

@Override
public void onAccuracyChanged(android.hardware.Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

}

享受:D