In Android (target 25) I have a background service and in onCreate function I have initialized a phone state listener. It works fine on Android versions that are before Nougat but in Nougat it doesn't work, even though the permissions are granted.
public class Service extends IntentService
{
class PhoneListener extends PhoneStateListener
{
String TAG = getClass().getName();
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG,"IDLE" );
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG,"OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG,"RINGING");
break;
}
}
}
public Service ()
{
super("ChatService");
}
public Service(String name)
{
super(name);
}
@Override
public void onCreate()
{
super.onCreate();
TelephonyManager tm = (TelephonyManager)getApplicationContext().getSystemService(TELEPHONY_SERVICE);
PhoneListener listener = new PhoneListener();
tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
I don't know what is the problem, it looks like the telephony manager is not registered and therefore the onCallStateChanged is not triggered.
One of my guesses is the Doze feature that was introduced on Android M, but still.. this code works fine on Android 6 even when the phone is not found "in work"
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…