1:申请权限
Manifest.permission.CALL_PHONE
2:创建intent
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3:获取 PhoneAccountHandle
above android.os.Build.VERSION_CODES.M 以上添加 PhoneAccountHandle。
/**
* 基于iccid 获取PhoneAccountHandle
*
* @param iccid sim卡iccid
*/
public static PhoneAccountHandle getPhoneAccountHandle(String iccid) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
return null;
}
try {
TelecomManager telecomManager = (TelecomManager) AppEnv.getContext().getSystemService(TELECOM_SERVICE);
if (telecomManager == null) {
return null;
}
List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
if (phoneAccountHandleList == null || phoneAccountHandleList.isEmpty()) {
return null;
}
for (PhoneAccountHandle handle : phoneAccountHandleList) {
if (iccid.equalsIgnoreCase(handle.getId())) {
return handle;
}
}
} catch (Throwable e) {
Log.e(TAG, "getPhoneAccountHandle error:", e);
}
return null;
}
PhoneAccountHandle phoneAccountHandle = null;
if (!TextUtils.isEmpty(iccId) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
phoneAccountHandle =getPhoneAccountHandle(iccId);
if (phoneAccountHandle != null) {
intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
}
}
4:添加soltId
PhoneAccountHandle==null 或者低于m .
public static final String[] dualSimTypes = {
"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"
};
for (String dualSimType : dualSimTypes) {
//所有的可能都加上
intent.putExtra(dualSimType, slotId);
}
5:拨打电话
Uri data = Uri.parse("tel:" + phone);
intent.setData(data);
try {
context.startActivity(intent);
return true;
} catch (Throwable e) {
Log.e(TAG, "startCall error: ", e);
}