增加加速计传感器灵敏度,并在手机摇动时增加亮度屏幕

时间:2012-05-13 07:23:05

标签: android android-layout android-intent android-widget

我想在这段代码中实现一些指令来增加加速计传感器的灵敏度,并在手机摇动时增加屏幕亮度

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,         sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
lastUpdate = System.currentTimeMillis();

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {                                     getAccelerometer(event);
}
}   
private void getAccelerometer(SensorEvent event) {
super.onResume();
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 2) //
{
if (actualTime - lastUpdate < 200) {            
return;
}           
lastUpdate = actualTime;            
Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT).show();  
if (mMediaPlayer.isPlaying() == false){ 
mMediaPlayer.start();               
}
}
}

1 个答案:

答案 0 :(得分:1)

int stretch = 50;
int boundary = 5;

float x = values[0];
float y = values[1];
float z = values[2];

x *= stretch;
y *= stretch;
z *= stretch;

if (x > 5 || x < -5) 
   x = 0;
if (y > 5 || y < -5) 
   y = 0;
if (z > 5 || z < -5) 
   z = 0;

只是一个非常基本的过滤器

就屏幕亮度而言:

private void setBrightness(int brightness) {  
try {  
  IHardwareService hardware = IHardwareService.Stub.asInterface(  
  ServiceManager.getService("hardware"));  
  if (hardware != null) {  
    hardware.setScreenBacklight(brightness);  
  }  
} catch (RemoteException doe) {            
  }          
}