7

Im using my phone for personal/work lines using dualsim (right now Xiaomi Mi5) with Android 7. Im looking for a way to use my work line (2nd sim) to gets turned on at 6am and gets off at 22.00 pm from Monday to Friday.

After looking for some apps, api in android to make it myself, could find the way to achieve it.

Thanks

KashMalaga
  • 77
  • 1
  • 6

1 Answers1

4

Software Solution

There is an App called Dual SIM Control that can manage the data and is compatible with Tasker however it costs 1.20 Euros. You can try the functionality in a free version (App no longer available). It claims to work without root but I don't know how it works.

Solution with Root

I researched a bit and found the following to turn off the data of one sim card in a dual sim phone with a SDK Version higher then 22:

Author: 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);
        }
    }
}

and

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;
        }
    }
}
camelCase1492
  • 642
  • 8
  • 17
Noah Mühl
  • 137
  • 2
  • 9
  • Appreciate your effort @Noah Muhl Cheers!! – Sreehari May 26 '17 at 07:43
  • haha why do you need a for loop for this. what you should have done is extract out your try-catch-finally block to a method and call that method twice with your 2 different su commands. -- not related to the question just pointing out bad programming. – tharinduwijewardane Sep 27 '19 at 08:43