2011-07-18 33 views
2

我使用广播接收器来捕获电话状态更改。 它在第一次(对于State_OffHook)状态改变时正常工作,但在通话结束时不会作出反应。 这是我的代码:Android:广播接收器中的电话状态

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);      
      if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {working fine} 
       else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {doesn't react} 

回答

0

当你有没有打电话给你是IDLE状态,当你得到一个呼叫它去OFFHOOK状态 ,当你再次调用结束它去IDLE状态

更多信息请参考 这

How to know whether I am in a call on Android?

Ë DIT:

@Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     super.onCallStateChanged(state, incomingNumber); 

     switch (state) { 
      case TelephonyManager.CALL_STATE_IDLE: 
       // Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show(); 
       if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) { 
        //Here you are came from offhook because value of UDF.phoneState != TelephonyManager.CALL_STATE_IDLE 
        //IDLE is calls many times so you have to keep track by a static variable like UDF.phoneState 
       } 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       //Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show(); 
       break; 
      case TelephonyManager.CALL_STATE_RINGING: 
       //Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show(); 
       endCallIfBlocked(incomingNumber); 
       break; 

      default: 
       break; 
     } 
     UDF.phoneState = state; 
    } 
+0

我知道,问题是我试图在手机中进行更改。当状态为OFF_HOOK时它的工作,以及当该电话改回IDLE时它不工作。我试图将代码更改为您发送的链接中的代码,但仍然存在同样的问题。 – ItayM

+0

我编辑了我的答案,如果你有问题,然后告诉我你究竟想要什么 – Dharmendra

+0

@Dharmendra,你只需要在'BroadcastReceiver'中找到手机状态。这就够了。即使你跟踪听众的状态,事情仍然会出错。我的意思是,按照你的方式保持轨道,但在'BroadcastReceiver'中。非常感谢 :-) – 2012-02-14 21:14:57