如何从Android应用程序连续两次调用

时间:2017-01-17 09:09:44

标签: android methods listener actionlistener phone-call

你们中的任何人都知道连续两次打电话怎么可能?点击一个按钮,应用程序应该拨打第一个号码,之后应该感觉到第一个呼叫刚刚结束 并自动拨打第二个号码。使用以下代码,它只调用第一个数字。

private void callBack(String phone) {
    Intent callIntent = new Intent(Intent.ACTION_CALL)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.setData(Uri.parse("tel:" + phone));
    callIntent.putExtra("com.android.phone.extra.slot", 1);
    startActivity(callIntent);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b = (Button) this.findViewById(R.id.CallButton);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          callBack("111111111");
          callBack("222222222");
       }});
}

2 个答案:

答案 0 :(得分:1)

你可以有一个广播接收者

public class EndCallReceiver extends BroadcastReceiver {

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

     Bundle bundle = intent.getExtras();
     String phoneNumber= bundle.getString("incoming_number");   
     if (phoneNumber == "first_number") {
         //call second one
     }     
  }

}

您必须在清单上注册

<receiver android:name=".EndCallReceiver" >
   <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
</receiver>

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

答案 1 :(得分:1)

Just Put Class接收和处理呼叫广播

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

            Context mContext;
            String incoming_nr;
            private int prev_state;

            @Override
            public void onReceive(Context context, Intent intent) {
                TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
                CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
                telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
                mContext = context;
            }
            /* Custom PhoneStateListener */
            public class CustomPhoneStateListener extends PhoneStateListener {

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

                    if (incomingNumber != null && incomingNumber.length() > 0) incoming_nr = incomingNumber;

                    switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING:
                            prev_state = state;
                            break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
                            prev_state = state;
                            break;
                        case TelephonyManager.CALL_STATE_IDLE:
                            if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
                                prev_state = state;
                                Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
                                //Answered Call which is ended  
                            }
                            if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                                prev_state = state;
                                //Rejected or Missed call  
                            }
                            break;

                    }
                }
            }
        } 

下面的许可并在呼叫电话的清单中注册接收者,如果结束则知道状态

<uses-permission android:name="android.permission.CALL_PHONE" />
   <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

         <receiver android:name=".PhoneStateBroadcastReceiver">
                    <intent-filter>
                        <action android:name="android.intent.action.PHONE_STATE"></action>
                    </intent-filter>
                </receiver>

注意: - 放一些条件拨打第二个号码,否则它应该进入无限循环。            - 您需要在acivitiy / fragment中注册/取消注册广播接收器,以便处理呼叫结束/重新启动第二个呼叫操作。