Tuesday, 27 November 2018

Programmatically disable the screen on/off magnetic sensor on Android

Several Android devices famously the Nexus series have a magnetic sensor that is NOT the android.hardware.Sensor.TYPE_MAGNETIC_FIELD used exclusively to turn the screen ON/OFF automatically using phone cases that have a small magnet inside them.

On my application, I'm using magnet detection using android.hardware.Sensor.TYPE_MAGNETIC_FIELD and the SensorManager to detect user touches the phone in a case with some magnets inside.

The code works without issues, the problem is that it's extremely easy on those devices to accidentally trigger the screen ON/OFF sensor thou, turning off the screen.

I've tried:

  • android:keepScreenOn="true" on the XML
  • using the permission android.permission.WAKE_LOCK and acquiring a wake-lock via PowerManager.

Both without success.

Is there a way to temporarily disable this sensor while my activity is resumed?



from Programmatically disable the screen on/off magnetic sensor on Android

No comments:

Post a Comment