Failed 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 permission code android.permission.READ_PHONE_STATEto receive a message with a toast, including the number of the call started. My problem is that the incoming number is empty, no matter if the call is outgoing or incoming, toasts appear, but are empty.
I saw some solutions for this, but the solution was to get the number in a completely different way, and I want to know if my code can be used for my needs.
Thanks for the advice,
Adam.

+5
source share
1 answer

Please indicate 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 null, . CALL_STATE_RINGING.

, .

+2

All Articles