I am trying to get outgoing number but before oreo the code is working absolutely fine but in devices with oreo code is not working.
public class IncomingBroadCastReceiver extends BroadcastReceiver {
Context context;
WhatsAppContacts requiredContact;
List<String> messages = new ArrayList<>();
String callerName="";
Realm realm;
String outgoingNumber = "";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("Search","onReceive");
this.context = context;
realm = Realm.getDefaultInstance();
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("Search","above Ringing"+state);
if(state == TelephonyManager.CALL_STATE_RINGING){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("number", incomingNumber);
Log.d("Ringing", incomingNumber);
editor.apply();
Log.d("Search","inside call state changed");
initialiseAndStartService();
}
else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
if(number!=null){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("number", number);
editor.apply();
Log.d("Search","inside call state changed");
initialiseAndStartService();
}
}
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
In versions before oreo it's working completely fine. But in Oreo not able to get outgoing number.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…