Android auto, make call, dial

Personal
CenterDownLoad

Android automatic call function can be realized through the following steps:
1. Add the call permission in the AndroidManifest.xml file:
```
<uses-permission android:name="android.permission.CALL_PHONE" />
```
n2. Use Intent in the code to start the calling interface:

```
String phoneNumber = "10086";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
```
Note: Use ACTION_CALL needs to be in AndroidManifest.xml Add the following permissions to the file:
```
<uses-permission android:name="android.permission.CALL_PHONE" /> ``
`
If you don’t want to make a call directly, but want to jump to the dial interface, you can use ACTION_DIAL:
`` `
String phoneNumber = "10086";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
```
In this way, the Android automatic dialing function can be realized.

Full code:


public class MainActivity extends AppCompatActivity {

    private EditText etPhone;
    private TextView tvTime;
    private final static String simSlotName[] = {
            "extra_asus_dial_use_dualsim",
            "com.android.phone.extra.slot",//基本上是这个
            "slot",
            "simslot",
            "sim_slot",
            "subscription",//华为是这个
            "Subscription",
            "phone",
            "com.android.phone.DialingMode",
            "simSlot",
            "slot_id",//小米是这个
            "simId",
            "simnum",
            "phone_type",
            "slotId",
            "slotIdx"
    };
    private Handler mHandler = new Handler();
    private Runnable runnable;
    private int time = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etPhone = findViewById(R.id.et_phone);
        tvTime = findViewById(R.id.tv_time);
        TextView tvCall1 = findViewById(R.id.tv_call1);
        TextView tvCall2 = findViewById(R.id.tv_call2);
        TextView tvHangUp = findViewById(R.id.tv_hang_up);
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            //未授权
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
        } else {
            //已授权
        }
        TelephonyManager tpm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        tpm.listen(new MyPhontStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
        runnable = new Runnable() {

            @Override
            public void run() {
                time--;
                if (time == 0) {
                    time = 5;
                    String phone = etPhone.getText().toString();
                    try {
                        /**
                         * Intent.ACTION_CALL 直接拨打电话
                         * Intent.ACTION_DIAL 跳转到拨号盘
                         */
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        for (int i = 0; i < simSlotName.length; i++) {
                            intent.putExtra(simSlotName[i], 0);
                        }
                        startActivity(intent);
                        Log.e("错误信息:", "跳转");
                    } catch (Exception e) {
                        Log.e("错误信息:", e.getMessage());
                    }
                } else {
                    tvTime.setText(time + "");
                    mHandler.postDelayed(this, 1000);
                }
            }
        };
        tvCall1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time = 5;
                tvTime.setText("5");
                mHandler.postDelayed(runnable, 500);
            }
        });
        tvCall2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etPhone.getText().toString();
                try {
                    /**
                     * Intent.ACTION_CALL 直接拨打电话
                     * Intent.ACTION_DIAL 跳转到拨号盘
                     */
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    for (int i = 0; i < simSlotName.length; i++) {
                        intent.putExtra(simSlotName[i], 1);
                    }
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e("错误信息:", e.getMessage());
                }
            }
        });
        tvHangUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        time = 5;
        tvTime.setText("5");
        mHandler.postDelayed(runnable, 500);
    }

    @Override
    protected void onDestroy() {
        mHandler.removeCallbacks(runnable);
        mHandler = null;
        super.onDestroy();
    }

    class MyPhontStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
            Log.e("电话状态:", "------------");
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE://空闲
                    Log.e("电话状态:", "空闲");
                    break;
                case TelephonyManager.CALL_STATE_RINGING://来电
                    Log.e("电话状态:", "来电");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK://正在通话中
                    Log.e("电话状态:", "正在通话中");
                    break;
            }
        }
    }
}

personal center

DownLoad

Guess you like

Origin blog.csdn.net/YDHIT/article/details/130746822