天天看点

84.android 简单的(双卡手机)指定某个SIM卡拨打电话//第一步 权限://Activity里使用: 

//第一步 权限:

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

//Activity里使用: 

//拨号请求码
public static final int REQUEST_CALL_PERMISSION = 10111;
           

//动态权限申请:

//打电话申请权限,
    public boolean checkReadPermission(String string_permission, int request_code) {
        boolean flag = false;
//已有权限
        if (ContextCompat.checkSelfPermission(this, string_permission) == PackageManager.PERMISSION_GRANTED) {
            flag = true;
        } else {
//申请权限
            ActivityCompat.requestPermissions(this, new String[]{string_permission}, request_code);
        }
        return flag;
    }

    //逻辑判断,是否允许打电话权限
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
//拨打电话
            case REQUEST_CALL_PERMISSION:
//失败,吐司
                if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "请允许拨号权限后再试", Toast.LENGTH_SHORT).show();
                } else {
//成功,直接调用开始拨打方法
                    call("tel:" + "10086");
                }
                break;
        }
    }


           

//初始化:

//指定SIM卡拨打

public static final String[] dualSimTypes = { "subscription", "Subscription",

        "com.android.phone.extra.slot",

        "phone", "com.android.phone.DialingMode",

        "simId", "simnum", "phone_type",

        "simSlot" };

//打电话的方法: 0代表卡1,1代表卡2

//申请到权限后打电话
    public void call(String telPhone) {
        if (checkReadPermission(Manifest.permission.CALL_PHONE, REQUEST_CALL_PERMISSION)) {
//            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(telPhone));
//            startActivity(intent);
            Intent callIntent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            callIntent.setData(Uri.parse(telPhone));
            for (int i=0; i < dualSimTypes.length; i++) {
                //0代表卡1,1代表卡2
                callIntent.putExtra(dualSimTypes[i], 0);
            }
            this.startActivity(callIntent);
        }
    }
           

//在点击事件里调用:

call("tel:" + "10086");
           

//----------------------------------------------------------完-----------------------------------------------------------------------