7

我使用带有 Android 7 的 dualsim(现在是小米 Mi5)将我的手机用于个人/工作线路。我正在寻找一种方法来使用我的工作线路(第二个 sim)在早上 6 点打开并在周一至下午 22 点下车星期五。

找了一些app,android中的api自己制作,可以找到实现的方法。

谢谢

4

1 回答 1

4

软件解决方案

有一个名为Dual SIM Control的应用程序可以管理数据并与 Tasker 兼容,但价格为 1.20 欧元。您可以在免费版本中试用该功能。它声称无需root即可工作,但我不知道它是如何工作的。

根解决方案

我研究了一下,发现以下是在SDK版本高于22的双卡手机中打开一张sim卡的数据:

作者:https ://stackoverflow.com/users/463053/chuongpham

public static void setMobileNetworkfromLollipop(Context context) throws Exception {
    String transactionCode = getTransactionCode(context);
    String command = null;
    SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    // Loop through the subscription list i.e. SIM list.
    for (int i = 0; i < mSubscriptionManager.getActiveSubscriptionInfoCountMax(); i++) {
        if (transactionCode != null && transactionCode.length() > 0) {
            // Get the active subscription ID for a given SIM card.
            int subscriptionId = mSubscriptionManager.getActiveSubscriptionInfoList().get(i).getSubscriptionId();
            // Execute the command via `su` to turn off
            // mobile network for a subscription service.
            command = "service call phone " + transactionCode + " i32 " + subscriptionId + " i32 " + Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0);
            executeCommandViaSu(context, "-c", command);
        }
    }
}

private static void executeCommandViaSu(Context context, String option, String command) {
    boolean success = false;
    String su = "su";
    for (int i=0; i < 3; i++) {
        // Default "su" command executed successfully, then quit.
        if (success) {
            break;
        }
        // Else, execute other "su" commands.
        if (i == 1) {
            su = "/system/xbin/su";
        } else if (i == 2) {
            su = "/system/bin/su";
        }
        try {
            // Execute command as "su".
            Runtime.getRuntime().exec(new String[]{su, option, command});
        } catch (IOException e) {
            success = false;
            // Oops! Cannot execute `su` for some reason.
            // Log error here.
        } finally {
            success = true;
        }
    }
}
于 2017-05-25T19:39:37.093 回答