andoird拨号流程简单解析

时间:2022-09-20 18:20:11

android拨号分两种,一种经过拨号器,输入号码,然后按CALL键拨打出去,还有一种经过联系人或者通话记录拨打出去。

 

第一种方式:TwelveKeyDialer.java中的placeCall()

void placeCall() {
        final String number = mDigits.getText().toString();
        if (number == null || !TextUtils.isGraphic(number)) {
            // There is no number entered.
            playTone(ToneGenerator.TONE_PROP_NACK);
            return;
        }
        Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
                Uri.fromParts("tel", number, null));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        mDigits.getText().clear();
        finish();
    }

定一个一个ACTION_CALL_PRIVILEGED,自动匹配上OutgoingCallBroadcaster

if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
            action = emergencyNumber
                    ? Intent.ACTION_CALL_EMERGENCY
                    : Intent.ACTION_CALL;
            intent.setAction(action);
        }

区分号码是否为紧急号码,是紧急号码的话立即起一个InCallScreen的Activity,不是的话发出广播,转发一个拨号Intent,

Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
        if (number != null) broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
        broadcastIntent.putExtra(EXTRA_ALREADY_CALLED, callNow);
        broadcastIntent.putExtra(EXTRA_ORIGINAL_URI, intent.getData().toString());
        if (LOGV) Log.v(TAG, "Broadcasting intent " + broadcastIntent + ".");
        sendOrderedBroadcast(broadcastIntent, PERMISSION, null, null,
                             Activity.RESULT_OK, number, null);

而OutgoingReceiver接收到广播以后,才起一个InCallScreen

Intent newIntent = new Intent(Intent.ACTION_CALL, uri);
        newIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
        newIntent.setClass(context, InCallScreen.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(newIntent);

这样,这个拨打的过程就完成了。

 

 

如果在历史通话记录界面,响应用户的click请求

protected void onListItemClick(ListView l, View v, int position, long id) {
    static final int COLUMN_INDEX_NUMBER = 4;                                           |        Intent intent = new Intent(this, CallDetailActivity.class);
                                                                                        |        intent.setData(ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI, id));
    @Override                                                                           |        startActivity(intent);
    protected void onCreate(Bundle icicle) {                                            |    }
起一个CallDetailActivity,在CallDetailActivity界面,如果当前电话是空闲的,直接发一个ACTION_CALL_PRIVILEGED给OutgoingCallBroadcaster

TelephonyManager tm = (TelephonyManager)                                |
                        getSystemService(Context.TELEPHONY_SERVICE);                    |            int callType = cursor.getInt(CALL_TYPE_COLUMN_INDEX);
                if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {            |            if (!number.startsWith("+") &&
                    Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,       |                    (callType == Calls.INCOMING_TYPE
                            Uri.fromParts("tel", mNumber, null));                       |                            || callType == Calls.MISSED_TYPE)) {
                    startActivity(callIntent);