I use a class that extends BroadcastReceiver. how can I get the phone number during an incoming or outgoing call?
This is my department:
public class MyCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context , Intent intent) {
if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_OFFHOOK )) {
//Call started
} else if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_IDLE )) {
//Call ended
} else if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_RINGING )) {
//Incoming call
}
} }
CodePudding user response:
you can get it this way.
public class CallRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
if (state == 1 || state == 2) {
if (phoneNumber != null && !phoneNumber.equals("")) {
Tİmber.e("Call Number: %s",phoneNumber);
}
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Timber.e("Error: %s", e.getLocalizedMessage());
}
}
}