Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get non-empty string from PhoneStateListener::onCallStateChanged

Source:

listener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            Toast toast = Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG);
            toast.show();
        }

    };

    ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(listener , PhoneStateListener.LISTEN_CALL_STATE )

Hi,
I am using this code, with android.permission.READ_PHONE_STATE permission, to get toast message including number of started call. My problem is that incomingNumber is empty, it doest not matter if call is outgoing or incomming, toast appear, but empty.
I have seen some solutions for this, but solution was to get the number with completely different way and I wanna know if my code can be used for my needs.
Thanks for advices,
Adam

like image 845
Adam Bogocz Avatar asked Dec 10 '10 18:12

Adam Bogocz


1 Answers

please include permission in your application.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />


if(state == TelephonyManager.CALL_STATE_RINGING){
Toast toast = Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG);
toast.show();
}

CALL_STATE_OFFHOOK return null if its first call. So you should use CALL_STATE_RINGING.

Hopefully it will help you.

like image 123
Cool Java guy מוחמד Avatar answered Nov 10 '22 11:11

Cool Java guy מוחמד