2014-02-17 58 views
0

如果有任何方法使用像「Normal_Delay,...」那樣的常數來改變其他的andriod傳感器的延遲。如何以編程方式設置Android傳感器的延遲?

我想提出的延遲值,因爲我喜歡。如果這是可能的?

其實,我找到了一個解決方案,通過使用「處理程序」,但它不工作。

這裏是處理器的解決方案,但它不起作用:

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

} 

像這樣

HandlerThread mHandlerThread創建處理程序; 處理程序處理程序;

mHandlerThread =新HandlerThread( 「sensorThread」);

+0

'通過使用 「處理程序」 找到了一個解決方案,但它不work.' ......您可以發佈代碼你嘗試過什麼? FYI-你不能改變不是'SENSOR_DELAY_FASTEST','SENSOR_DELAY_GAME','SENSOR_DELAY_UI'其他傳感器延遲時間和'SENSOR_DELAY_NORMAL' ... –

+0

好的嘗試這些... SENSOR_DELAY_UI,SENSOR_DELAY_GAME,或SENSOR_DELAY_FASTEST –

+0

感謝您的回覆,我會立即發佈@GopalRao –

回答

2

最後,我創建了一個解決方案來解決這個問題,它通過使用澄清This現在你可以設置自定義的延遲將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

+0

是的,它的工作:) –

+0

很棒...享受你的編碼:D –

相關問題