TelephonyManager.EXTRA_STATE_OFFHOOK状态中的变量值为null

时间:2014-08-22 08:44:06

标签: android

我想在接听电话号码或TelephonyManager.EXTRA_STATE_OFFHOOK状态下执行一些操作。我在

中获得空值
 if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){}

我的代码如下 -

    // 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.

                }
            }

我该怎么做?

1 个答案:

答案 0 :(得分:1)

这意味着您收到的广播TelephonyManager.EXTRA_STATE中没有字符串额外匹配Intent

防止空指针的最佳方法是将if语句重新排列为:

if(TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))

然后,只有在TelephonyManager.EXTRA_STATE_OFFHOOK出现时才会处理它。