I had this problem also trying to run a PhoneStateListener in a service, and it seems like it would just 'drop' only on N devices.
I tried a lot of different suggestions but I kept losing the listener on my nexus5x running 7.0. The way that I was able to keep it alive 100% of the time was by having the service run a Foreground notification. Basically it stays alive in the notification tray as long as the service is alive. This keeps my service alive through different phone states where it would drop the listener before, such as when a, outgoing call was answered. As long as you don't set a sound or vibrate to the notification builder, it is pretty much unnoticeable. My services onCreate looks like this:
MyPhoneListener phoneStateListener = new MyPhoneListener(getApplicationContext());
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_favorites) //status bar icon
.setContentTitle("Title") //whatever title
.setContentText("Stuff") //main notification text
.setContentIntent(pendingIntent).build();
startForeground(12345, notification);
and then you remove the notification in the onDestroy of the service:
@Override
public void onDestroy() {
Log.i("log", "service ending");
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.cancel(12345);
}
Hope this helps!