如何知道何时拨打和连接?

时间:2013-07-19 23:01:01

标签: android broadcastreceiver telephonymanager

你好我使用一个广播接收器试图找出一个呼叫被“制造”的时间,并且在呼叫被“制造”之后,当所做的呼叫被“连接”时,我搜索并提出了这个代码但是这个工作当接听电话和拨打电话时,我真正需要的就是拨打电话和连接电话时,任何帮助指导我的工作都表示赞赏,谢谢你的时间

receiver = new BroadcastReceiver() {
               @Override
               public void onReceive(Context context, Intent intent) {
                  String action = intent.getAction();
                  TelephonyManager telephonyManager = (TelephonyManager) context
                          .getSystemService(Context.TELEPHONY_SERVICE);

                  if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
                       //action for phone state changed
                      Toast.makeText(context, "Receiver call status", Toast.LENGTH_SHORT).show();
                      Log.d("reciever","entered keep going");
                      if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
                          Toast.makeText(context, "Receiver call connected", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(context, "Receiver call not connected", Toast.LENGTH_SHORT).show();  
                        } 
                  }     
               }
            };
            IntentFilter filter = new IntentFilter();

            filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);

            registerReceiver(receiver,filter);

1 个答案:

答案 0 :(得分:2)

Here你可以找到所有的通话状态,只需处理这种状态,就像我做的那样:

public class PhoneState extends PhoneStateListener {

@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            //Some Action
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //Some Action
            break;
        //case TelephonyManager.ANOTHER_STATE: ...
    }
    }
}