How to set delay of Android Sensors programmatically?

北战南征 提交于 2019-12-11 23:15:21

问题


If there any method to change delay of andriod sensor other using constant like "Normal_Delay, ...".

I want to put the delay value as I like. if this is possible ?

Actually, I found a solution by using "handler" but it doesn't work.

Here is the solution with Handler but it doesn't work:

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);
    }

}

and creating Handler like this

HandlerThread mHandlerThread; Handler handler;

mHandlerThread = new HandlerThread("sensorThread");


回答1:


Finally, I created a solution to solve this problem and it works perfect with me by using clarification of This now you can set custom delay for your android sensors..!

Implement it as following:

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

}

}

Enjoy :D



来源:https://stackoverflow.com/questions/21829972/how-to-set-delay-of-android-sensors-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!