Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

java - In Oreo, I'm not able to get an outgoing number

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a limitation in oreo. You can't register implicit broadcast in manifest and NEW_OUTGOING_CALL is an implicit broadcast. You can only get that broadcast work when your app is live and user is interacting with it. you can find more information here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...