Android Studio - 电话

时间:2016-04-07 10:15:59

标签: android phone-call

我想制作一个通过按下按钮进行多个电话呼叫(一个接一个)的应用程序。我有一个包含电话号码的阵列。

这是我目前的代码:

public class MainActivity extends Activity {

String[]numbers={
        "number1",
        "number2"};


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < numbers.length;i++) {

                if(TelephonyManager.CALL_STATE_RINGING) {
                    dialContactPhone(numbers[i]);
                }
            }
        }
    });
}

private void dialContactPhone(final String phoneNumber) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    startActivityForResult(new Intent(Intent.ACTION_CALL, Uri.fromParts("tel", phoneNumber, null)));
}

private void startActivityForResult(Intent tel) {

}

}

2 个答案:

答案 0 :(得分:0)

You should implement this example.
And after first call is finished start next.

答案 1 :(得分:0)

You are only able to make one call because you are dialling both the numbers at the same time.

You should make the first call, then wait for that call to end and then make the next call.

private class EndCallListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if(TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }
        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
            //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
            Log.i(LOG_TAG, "OFFHOOK");
        }
        if(TelephonyManager.CALL_STATE_IDLE == state) {
            //when this state occurs, and your flag is set, restart your app
            Log.i(LOG_TAG, "IDLE");

            // Make your next call from here.
        }
    }
}

READ_PHONE_STATE permission required.