-1

你好,我正在使用广播接收器来尝试找出何时“拨打”电话,以及在“拨打”电话后,当拨打电话“已连接”时,我四处搜索并想出了这段代码,但这在接收时有效打电话和打电话,我真正需要的是当打电话和接通时,任何帮助指导我的人都非常感谢,谢谢你的时间

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);
4

1 回答 1

2

在这里您可以找到所有呼叫状态,只需像我一样处理这些状态:

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: ...
    }
    }
}
于 2013-07-19T23:10:03.380 回答