Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect motion when screen is off?

My app simply need to detect any motion of the device while screen is off.

I know that accelerometer is used for this task but it don't work while screen is off in all devices. this is a list of devices http://www.saltwebsites.com/2012/android-accelerometers-screen-off

so is there a way of taking accelerometer sensor data while screen is off that works on all devices?

or is there a way to detect motion using another sensors?

like image 974
Ahmed Hegazy Avatar asked Jul 01 '13 09:07

Ahmed Hegazy


People also ask

What are the three ways to detect motion?

Motion can be detected by monitoring changes in: Infrared light (passive and active sensors) Visible light (video and camera systems) Radio frequency energy (radar, microwave and tomographic motion detection)

How does a phone detect motion?

The Android platform provides several sensors that let you monitor the motion of a device. The sensors' possible architectures vary by sensor type: The gravity, linear acceleration, rotation vector, significant motion, step counter, and step detector sensors are either hardware-based or software-based.

Can my phone detect movement?

Use Motion Detector Android App Motion Detector app is another free Android third-party application that offers motion detection with the help of your phone's camera. This is a simple app that shows the number of times motion is detected through your camera and also takes photos of the motion to save it on your device.


1 Answers

Partial Wake Lock is all you need to access accelerometer readings while the screen is off.

You can use it like this:

private PowerManager.WakeLock mWakeLock;

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
mWakeLock.acquire();

And after you're done, just release the lock:

mWakeLock.release();

If you obtain accelerometer data in a Service, you could simply acquire lock in it's onCreate() and release in onDestroy().

like image 186
alex Avatar answered Sep 20 '22 00:09

alex