1. 程式人生 > >84.android 指定哪個SIM卡撥打電話

84.android 指定哪個SIM卡撥打電話

//第一步 許可權:

<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("tel:" + telPhone));
            for (int i=0; i < dualSimTypes.length; i++) {
                //0代表卡1,1代表卡2
                callIntent.putExtra(dualSimTypes[i], 0);
            }
            this.startActivity(callIntent);
        }
    }

//在點選事件裡呼叫:

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

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