PPB
PPB

Reputation: 161

PhoneStateListener is sometimes not responding in Nougat

I'm registering my PhoneStateListener in onStartCommand of my service. It was working perfectly in below android N devices. But sometimes its not responding in android N devices. Is it related to doze mode? If yes how to tackle it?

    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    CustomPhoneStateListener phoneStateListener = new CustomPhoneStateListener();
    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Upvotes: 1

Views: 1471

Answers (4)

Sanjay Gupta
Sanjay Gupta

Reputation: 11

I had this problem and resolved it after wasting 2-3 days with allowing the app to auto launch from Settings -> Permissions -> Manage auto launch

Upvotes: 0

shelll
shelll

Reputation: 3373

Or register your receiver in the manifest and enable/disable it in code when needed via PackageManager https://stackoverflow.com/a/6529365/671580

Upvotes: 0

mohit kejriwal
mohit kejriwal

Reputation: 1835

I have found a solution: One of my app was facing the same problem in Noughat. While debugging I found that the state listener is getting called only once and that too when the activity is created. As soon as I make any call the activity goes in background and there after listener was never called, hence I deduced that the listener I attached in onCreate is getting detached when I leave the activity so "Attach your State Listener in onResume". Your problem will be solved.

onResume() {
    PhoneCallListener p = new PhoneCallListener;
    TelephonyManager t = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    t.listen(p,PhoneStateListener.LISTEN_CALL_STATE);
}

Upvotes: 0

Nick Friskel
Nick Friskel

Reputation: 2437

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!

Upvotes: 1

Related Questions