如何以编程方式设置Android Sensors的延迟? [英] How to set delay of Android Sensors programmatically?
问题描述
如果有任何方法可以使用"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 =新的HandlerThread("sensorThread");
最后,我创建了一个解决此问题的解决方案,并且通过使用 } 享受:D 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: and creating Handler like this HandlerThread mHandlerThread;
Handler handler; mHandlerThread = new HandlerThread("sensorThread"); 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: } Enjoy :D 这篇关于如何以编程方式设置Android Sensors的延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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);
}
}
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
}