Android 自动、拨打电话、拨号

个人中心
DownLoad

Android 自动拨打电话功能可以通过以下步骤实现:
1. 在 AndroidManifest.xml 文件中添加拨打电话的权限:
```
<uses-permission android:name="android.permission.CALL_PHONE" />
```
n2. 在代码中使用 Intent 启动拨打电话的界面:

```
String phoneNumber = "10086";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
```
注意:使用 ACTION_CALL 需要在 AndroidManifest.xml 文件中添加以下权限:
```
<uses-permission android:name="android.permission.CALL_PHONE" />
```
如果不想直接拨打电话,而是想跳转到拨号界面,可以使用 ACTION_DIAL:
```
String phoneNumber = "10086";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
```
这样就可以实现 Android 自动拨打电话功能了。

完整代码:


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;
            }
        }
    }
}

个人中心

DownLoad

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android-Developer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值