如何在Android中选择电话

时间:2017-04-28 07:27:14

标签: android permissions android-phone-call

当我点击电话呼叫按钮时,如何选择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"/>

3 个答案:

答案 0 :(得分:1)

在开始意图之前重置应用程序首选项

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

答案 1 :(得分:1)

你需要创建一个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);

这是一个sim插槽帮助程序类,通过使用电信管理器为两个sims获取电话帐户句柄列表

<强>码

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

答案 2 :(得分:0)

尝试使用.ACTION_DIAL而不是.ACTION_CALL。这将打开一个Dialog选择器,其中设备中安装了可以调用的应用程序。

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