Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneStateListener onSignalStrengthsChanged stops being called in Service

I'm writing simple application containing two elements - Service and Activity. Activity is used only for starting and stopping the service. Service uses PhoneStateListener to get information about current CellID, LAC and RSSI (signal strength) and log them into file.

Everything works fine while phone is not sleeping. I've noticed that after switching off the screen and few minutes of inactivity my PhoneStateListener is not invoked anymore. I tried adding PARTIAL_WAKE_LOCK (and other types of locks too) into my service, but I guess it causes severe battery drain and shouldn't be used that way. On the other hand it didn't really help me though. My another try was to use Timer and unregister/register my PhoneStateListener in specified intervals. Unexpectedly, it looks that this way helped with refreshing CellId and LAC (so I guess that onCellLocationChanged() from my listener was called), but the signal strength still wasn't updated.

I didn't try using BroadcastReciever, but after some digging into topic and reading threads on SO I don't think it would work for me.

To sum up, my question: Why are PhoneStateListener methods not called when phone is in sleep? Is there any way (like force phone to wake up or to use different mechanism) to overcome this behavior efficiently?

like image 589
mih Avatar asked Dec 05 '11 12:12

mih


2 Answers

When the screen is off, android stops notifying about cell and signal changes. It does so by disabling URC notifications on the RIL level. As far as I know, you can't get updates without turning the screen on. A broadcast receiver wouldn't work any differently.

So if it is practical for you, I would suggest to have the screen turn on whenever you want to check these values. You can do that by acquiring a wake lock and providing the flags: ACQUIRE_CAUSES_WAKEUP | SCREEN_DIM_WAKE_LOCK

Edit: Re-registering the listener will in fact trigger the location change to be executed, but it doesn't pass the current cell info, only the last known cell info prior to the screen going off. To get the current cell info, you will still need to turn the screen on.

like image 127
black Avatar answered Oct 03 '22 19:10

black


call at the start:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "call_lock");

and at the end:

wakeLock.release();

you need the permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />
like image 37
Bush Avatar answered Oct 03 '22 17:10

Bush