I want to make application like TrueCaller. For that i need to get event of incoming call. For that i make Broadcast Receiver and it works fine till app is going to be killed.
So i want to make run my Broadcast receiver even if app is killed.
Previously i used Service for achieve this but in After Android O Service is not working. I thought that i can archive it by using WorkManager but i couldn't understand how to do it. So i want Service alternative or right way to archive this.
Please help me to do it.
Thanks in advance.
CallEventBroadcastReceiver.java
public class CallEventBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
try {
if (state == TelephonyManager.CALL_STATE_RINGING) {
Log.e("phoneNumber", phoneNumber);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}