Android Question Mobile Data On/Off - Lollipop +

Discussion in 'Android Questions' started by PhilipK, Jun 8, 2015.

  1. PhilipK

    PhilipK Member Licensed User

    Hi B4A

    Apparently, it is not now possible to toggle the Mobile Data Connection using the Toggle Library with Android L + . Let's say you want to conserve battery by turning the connection off and turn it on only for new data/messages from your app. etc., you can't. Tough custard!

    I have found a thread from StackOverflow that says:

    UPDATE 3: Many Android developers have emailed me questions regarding switching mobile network on/off for Android 5+, but instead of answering individual emails, I'll post my answer here so everyone can use it and adapt it for their Android apps.

    First thing first, let's clear up some misconception and misunderstanding regarding:

    svc data enable
    svc data disable

    The above methods would only turn background data on/off, not the subscription service, so the battery will drain a fair bit since the subscription service- an Android system service- will still be running in the background. For Android devices supporting multiple sim cards, this scenario is worse as the subscription service constantly scans for available mobile network(s) to use with the active SIM cards available in the Android device. Use this method at your own risk.

    Now, the proper way to switch off mobile network, including its corresponding subscription service via the SubscriptionManager class introduced in API 22, is:

    Code:
    public static void setMobileNetworkfromLollipop(Context context) throws Exception {
        
    String command = null;
        int state = 
    0;
        
    try {
            // Get the current state of the mobile network.
            state = isMobileDataEnabledFromLollipop(context) ? 
    0 : 1;
            // Get the value of the 
    "TRANSACTION_setDataEnabled" field.
            
    String transactionCode = getTransactionCode(context);
            // Android 
    5.1+ (API 22and later.
            
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                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 " + state;
                        executeCommandViaSu(context, 
    "-c", command);
                    
    }
                }
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                // Android 5.0 (API 21) only.
                if (transactionCode != null && transactionCode.length() > 0) {
                    // Execute the command via `su` to turn off mobile network.                    
                    command = "service call phone " + transactionCode + " i32 " + state;
                    executeCommandViaSu(context, "-c", command);
                }
            }
        } catch(Exception e) {
            // Oops! Something went wrong, so we throw the exception here.
            throw e;
        }          
    }
    To check if the mobile network is enabled or not:

    Code:
    rivate static boolean isMobileDataEnabledFromLollipop(Context context) {
        boolean state = 
    false;
        
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            state = 
    Settings.Global.getInt(context.getContentResolver(), "mobile_data"0) == 1;
        
    }
        return state;
    }
    The rest of the post: http://stackoverflow.com/questions/...-no-longer-callable-as-of-android-l-and-later

    If this method works, please could you clever guys wrap it up in a library or other?

    Many thanks.
     
  2. DonManfred

    DonManfred Expert Licensed User

    You need a rooted device for it. The code you posted calls a method named executeCommandViaSu and i guess su is SU (SuperUser) which is only available on rooted devices.

    Quote from stackoverflow
     
    hookshy likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice