Accelerometer stops sample delivery when screen is off on Droid / Nexus One even with WakeLock

I have a code that extends the service and registers the readings of the SensorEvent accelerometer sensor (SensorEvent) on Android. I would like to be able to record these sensor readings even when the device is turned off (I use battery power carefully, and this became apparent when it works). While the screen is working fine it works on 2.0.1 Motorola Droid and 2.1 Nexus One.

However, when the phone goes into sleep mode (by pressing the power button), the screen turns off and onSensorChanged events cease to be delivered (it is checked using the Log.e message, which is called every N times onSensorChanged ).

The service receives wakeLock to ensure that it will run in the background; but it does not seem to have any effect. I tried all the different PowerManager. locks, but none of them matter.

 _WakeLock = _PowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); _WakeLock.acquire(); 

There have been conflicting reports about whether you can actually receive data from sensors while the screen is off ... does anyone have experience with this in a more modern version of Android (Eclair) and hardware?

This seems to indicate that he worked for Cupcake: http://groups.google.com/group/android-developers/msg/a616773b12c2d9e5

PS: The exact same code works as expected in 1.5 on G1. Logging continues when the screen turns off, when the application is in the background, etc.

+23
android accelerometer android-2.0-eclair power-management
Jan 26
source share
5 answers

We concluded that it started with version 2.0.1. This seems to be intentional, perhaps part of the extended battery life that was touted as a feature. We had a working jitter to wake or unlock on 2.0, then it broke in the update, and we could not get any solution .; (It doesn’t matter if the processor is partially blocked, which should always prevent the processor from sleeping. From what I saw when debugging debugging via USB, sometimes there is a mention of changes in the sensor listener when the sleep mode occurs.

The user posted a workaround that he claimed works on Motorola devices - https://sites.google.com/a/bug-br.org.br/android/technical-documents

I tested the workaround by entering the following code from the tutorial and some manual revision (the code contains a few errors code):

 public class ShakeWakeupService extends Service implements SensorEventListener{ private Context mContext; SensorManager mSensorEventManager; Sensor mSensor; // BroadcastReceiver for handling ACTION_SCREEN_OFF. public BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Check action just to be on the safe side. if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { Log.v("shake mediator screen off","trying re-registration"); // Unregisters the listener and registers it again. mSensorEventManager.unregisterListener(ShakeWakeupService.this); mSensorEventManager.registerListener(ShakeWakeupService.this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); } } }; @Override public void onCreate() { super.onCreate(); Log.v("shake service startup","registering for shake"); mContext = getApplicationContext(); // Obtain a reference to system-wide sensor event manager. mSensorEventManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); // Get the default sensor for accel mSensor = mSensorEventManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // Register for events. mSensorEventManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); // Register our receiver for the ACTION_SCREEN_OFF action. This will make our receiver // code be called whenever the phone enters standby mode. IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); registerReceiver(mReceiver, filter); } @Override public void onDestroy() { // Unregister our receiver. unregisterReceiver(mReceiver); // Unregister from SensorManager. mSensorEventManager.unregisterListener(this); } @Override public IBinder onBind(Intent intent) { // We don't need a IBinder interface. return null; } public void onShake() { //Poke a user activity to cause wake? } public void onAccuracyChanged(Sensor sensor, int accuracy) { //not used right now } //Used to decide if it is a shake public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return; Log.v("sensor","sensor change is verifying"); } } 

The workaround works for me, but it doesn't work while I run screebl, which is a feature that many of my users really want to work along with what I'm developing.

+8
Jan 30 '10 at 8:44
source share

I just updated my Nexus One with the FROYO FRF50 ROM, which has been surfing the net for the past few days, and it seems that now the sensors work again when the screen is off, if you have PARTIAL_WAKELOCK. I do not know if this will lead to an official assembly, although I understand that this is based on the official assembly, which some N1 users received. Perhaps they changed their mind (again) and turned on the sensors again when the phone is locked. Fingers crossed.

Of course, they could just turn them off again in 2.2-r1, who knows ...

+1
May 23 '10 at 10:12
source share

Using SCREEN_DIM_WAKE_LOCK worked for me on HTC EVO ..

 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); _WakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG"); _WakeLock.acquire(); 
+1
Nov 16 '11 at 20:27
source share

It seems like this is a problem with firmware 2.1, I need it to be fixed, but I need to work in all kinds of workarounds to cover all versions.

I have compiled a list of phones and firmware that work / don't work / work intermittently so that we can either test the phone or the firmware.

http://apprssd.com/2010/09/08/android-onsensorchanged-not-working-when-screen-lock-is-on/

If you have a new phone, you can update the list, let me know.

0
Sep 10 '10 at 6:10
source share

There is an alternative solution (view) in which you constantly turn on the screen using:

Settings.System.putInt (getContentResolver (), Settings.System.SCREEN_OFF_TIMEOUT, -1);

But this does not prevent the user from turning off the screen by pressing the power button, and then the flow of sensor events will stop. You will also need permission from WRITE_SETTINGS for this.

The workaround above re-registration for sensor events does not work on Droid Eris, which is running Android 2.1 and is expected to never receive an update. I used the following, although it seems to work. Instead of unregistering and re-registering, this approach turns the screen back on when the user turns it off, although it returns to darkened. In the following code, a handler is just a simple handler created in an Activity (i.e. Handler = new Handler ();) and mTurnBackOn is a WakeLock initialized to zero.

 public BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { // Turn the screen back on again, from the main thread handler.post(new Runnable() { public void run() { if(mTurnBackOn != null) mTurnBackOn.release(); mTurnBackOn = mPwrMgr.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "AccelOn"); mTurnBackOn.acquire(); }}); } } }; 
0
Oct. 24 2018-10-10
source share



All Articles