PhoneStateListener changes are not handled
I need to intercept changes in the signal strength on an android device. I've seen different approaches arou开发者_如何学Pythonnd in StackOverflow. In my case, it's an Android Background Service that is trying to find those changes. However, I can't see what I'm doing wrong but it looks like the changes are not intercepted and I've put the phone in different metal boxes to see if it worked. By going to settings and looking at the values there, it looks like the OS is aware of those transitions.
public class SignalStateListener extends PhoneStateListener{
private int signalStrength;
public SignalStateListener (int signalStrength){
this.signalStrength = signalStrength;
}
@Override
public void onSignalStrengthChanged(int asu){
Log.e(TAG, "[[[[[Signal strength changed]]]]]]");
this.signalStrength = asu;
}
public int getSNR(){
return this.signalStrength;
}
}
//in onCreate()
tm = (TelephonyManager) getSystemService (Context.TELEPHONY_SERVICE);
listener = new SignalStateListener(0);
tm.listen(listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
private TelephonyManager tm = null;
private SignalStateListener listener = null;
I had the same kind of issue: after some time it seemed that the listening was dropped. To solve this, I created a Timer that repeats every 30 minutes the call for listening: create a Timer that calls for a TimerTask class inside the service which contains the listening requests and make the Timer call for the TimerTask class every X milliseconds. That worked for me.
精彩评论