Your service is getting cleaned up before the callback gets invoked. You should not really be relying on anything allocated in your BroadcastReceiver
to exist after onReceive
exits. You should put CustomPhoneStateListener
in Service
or Activity
. Then you can use an Intent
to launch the activity or service to do your state monitoring.
From the BroadcastReceiver
docs,
Once you return from onReceive(), the BroadcastReceiver is no longer active, and its hosting process is only as important as any other application components that are running in it. This is especially important because if that process was only hosting the BroadcastReceiver (a common case for applications that the user has never or not recently interacted with), then upon returning from onReceive() the system will consider its process to be empty and aggressively kill it so that resources are available for other more important processes.
The reason it is working on the emulator is probably because there are less processes in general on the emulator and for some reason it seems less aggressive about killing off processes.