如何选择 Phone 调用 Android

How to choose a Phone Call in Android

当我点击phone通话按钮时,如何选择skype、viber、sim1或sim2等。现在,它被 sim2 调用。我要选择。我在 Google 上搜索,没有找到我的问题。

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456789"));
try {
    startActivity(intent);
} catch (Exception e) {
    e.printStackTrace();
}

清单中的权限

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

尝试使用 .ACTION_DIAL 而不是 .ACTION_CALL 。这将打开一个对话框选择器,其中安装在设备中的应用程序具有调用功能。

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + "123456789"));
try {
    startActivity(intent);
} catch (Exception e) {
    e.printStackTrace();
}

在启动 intent 之前重置应用偏好

Settings->Apps/Application Manager -> Default/Downloaded Apps -> Click on overflow icon (i.e. three dots icon on top right of the screen) -> Reset App Preferences.

您的代码看起来不错。

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + "123456789"));
try {
    startActivity(intent);
} catch (Exception e) {
    e.printStackTrace();
}

您需要创建一个 sim 选择器对话框并设置 sim 1 和 sim 2 选项,并根据选择的 sim 号码设置 simNumber 变量(sim1 为 0,sim2 为 1)

这是我为从特定 SIM 卡(即 SIM 卡 1 或 SIM 卡 2)调用而实现的代码。

代码:

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

int simNumber = 0 or 1; //0 for sim1 and 1 for sim2
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + phoneNumber));
                callIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//Add slots here since different device needs different key so put all together
for (String s : simSlotName)
    intent.putExtra(s, simNumber);

//This will only work on API 22 or up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    intent.putExtra("android.telecom.extra.PHONE_ACCOUNT_HANDLE", (Parcelable) SimSlotHelper.getAccountHandles(context).get(simNumber))

context.startActivity(intent);

这是一个 class 的 sim 插槽助手,它将通过使用两个 sims 的电信管理器获得 phone 帐户句柄列表

代码:

public class SimSlotHelper {
public static List getAccountHandles(Context context) {
    Class c;
    Method m;
    TelecomManager telecomManager;
    List<PhoneAccountHandle> accountHandles;
    TelephonyManager telephony;
    telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {

        c = Class.forName("android.telecom.TelecomManager");
        Method m1 = c.getMethod("from", Context.class);
        telecomManager = (TelecomManager) m1.invoke(null, context);
        m = c.getMethod("getCallCapablePhoneAccounts");
        accountHandles = (List<PhoneAccountHandle>) m.invoke(telecomManager);
        return accountHandles;

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
 }
}