1

如果有任何方法可以改变安卓传感器的延迟,其他使用常量,如“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);
    }

}

并像这样创建处理程序

处理程序线程 mHandlerThread;处理程序处理程序;

mHandlerThread = new HandlerThread("sensorThread");

4

1 回答 1

4

最后,我创建了一个解决方案来解决这个问题,它通过使用澄清对我来说很完美,现在您可以为您的 android 传感器设置自定义延迟..!

实现如下:

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

于 2014-02-19T20:23:38.973 回答