Ramashanker Tripathi
Ramashanker Tripathi

Reputation: 537

Variable value is null inside TelephonyManager.EXTRA_STATE_OFFHOOK state

I want to perform some operation with calling number on call received or TelephonyManager.EXTRA_STATE_OFFHOOK state. I am getting null value inside the

 if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){}

My code is as below-

    // onReceive function of the Broadcast Receiver
        public void onReceive(Context context, Intent intent) {
                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

             String incommingCall = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);                  


                Toast.makeText(context,"Call from "+ incommingCall, Toast.LENGTH_LONG).show();

// value of **incomingcall** is present like..+918000000000. etc

                if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){

                Toast.makeText(context,"Call from extra_state_offHOOk "+ incommingCall, Toast.LENGTH_LONG).show();

// value of **incomingcall** is null.

                }
            }

how can I do this?

Upvotes: 0

Views: 951

Answers (1)

HexAndBugs
HexAndBugs

Reputation: 5799

This means that there is no String extra matching TelephonyManager.EXTRA_STATE in the Intent for the broadcast you've received.

The best way to protect against the null pointer is to rearrange you if statement to:

if(TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))

Then, you'll handle it only when TelephonyManager.EXTRA_STATE_OFFHOOK is present.

Upvotes: 1

Related Questions