如何在运动传感器检测到某些东西时打开屏幕?

时间:2015-07-08 11:30:41

标签: java android

当运动传感器检测到电话移动时,有没有办法打开屏幕? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

考虑这样的事情

//implement SensorEventListener
public class SensorActivity extends Activity, implements SensorEventListener {
......

SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

sensorMan.registerListener(context, sensor,
    SensorManager.SENSOR_DELAY_UI);

@Override
 public void onSensorChanged(SensorEvent event) {

     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){

      }
     //Or some other sensor.
  }  

文档here

中有更多信息

答案 1 :(得分:2)

这是为了打开屏幕,

    public void turnOnScreen() {
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        if (!powerManager.isScreenOn()) {
            PowerManager.WakeLock screenWakeLock = powerManager.newWakeLock(  PowerManager.ACQUIRE_CAUSES_WAKEUP
                                                   | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "tag");
            screenWakeLock.acquire();
            screenWakeLock.release();
        }
    }