2012-07-17 67 views
20

我想知道如何在被叫方接通電話時發出警報。我已經使用PhoneStateListener以及BroadcastReceiver如何瞭解被叫方是否接聽了電話(當他解除電話時,電話狀態是什麼)

通常它有三個狀態CALL_STATE_IDLE , CALL_STATE_OFFHOOK, CALL_STATE_RINGING

CALL_STATE_OFFHOOK當呼叫正在連接時狀態正在呼叫,被叫方應答 呼叫後,上述三種狀態均未被呼叫。

這是我的BroadcastReceiver。

public class PhoneStateBroadcastReceiver extends BroadcastReceiver 
{ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 

     TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 

    } 

    public class CustomPhoneStateListener extends PhoneStateListener 
    { 

     Context context; //Context to make Toast if required 
     ActivityManager activityManager; 

     public CustomPhoneStateListener(Context context) 
     { 
      super(); 
      this.context = context; 
     } 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) 
     { 
      super.onCallStateChanged(state, incomingNumber); 
      Log.v("PhoneStateBroadcastReceiver", "onCallStateChanged state"+state); 


      switch (state) 
      { 
      case TelephonyManager.CALL_STATE_IDLE: 
       Toast.makeText(context, "=CALL_STATE_IDLE==", Toast.LENGTH_LONG).show(); 
      break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       Toast.makeText(context, "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show(); 

       break; 
      case TelephonyManager.CALL_STATE_RINGING: 
       Toast.makeText(context, "CALL_STATE_RINGING", Toast.LENGTH_LONG).show(); 

       break; 
      default: 
       break; 
      } 
     } 
     } 
} 

我看到有些應用程序在接聽電話時錄音。我想知道接聽電話的狀態。

是否有任何其他狀態或聽衆知道被叫方何時接聽電話?

回答