0

I just rooted my device (verified :) ) and installed Linage 16 (Android 9).

The reason I did this is because I need to disable my mobile network and then reenable it to get a new and fresh IP address from my mobile service provider. This all has to be done automatically from within the app itself. So, manually disconnecting and reconnecting is not a solution for my particular case.

Therefore I did some research and I found for example this here that is supposed to enable mobile network toggling: https://stackoverflow.com/a/31488730/1020704

And this that explains how to request root privileges: https://stackoverflow.com/a/31488730/1020704

And this here that explains how to correctly setup the Manifest.xml: https://stackoverflow.com/a/31488730/1020704

So, what I get is something like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.example.app"
            xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" tools:ignore="ProtectedPermissions"/>


<application....

And in my code I do the following:

 public void toggleMobileNetwork(boolean mobileDataEnabled) {
    try {
        Process root = Runtime.getRuntime().exec("su");
        TelephonyManager telephonyService = (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE);
        Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
        if (null != setMobileDataEnabledMethod) {
            setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
        }
    } catch (Exception ex) {
        ex.getCause().printStackTrace();
    }
}

When I execute this code I get the following exception:

W/System.err: java.lang.SecurityException: setUserDataEnabled
W/System.err:     at android.os.Parcel.createException(Parcel.java:1950)
W/System.err:     at android.os.Parcel.readException(Parcel.java:1918)
W/System.err:     at android.os.Parcel.readException(Parcel.java:1868)
W/System.err:     at com.android.internal.telephony.ITelephony$Stub$Proxy.setUserDataEnabled(ITelephony.java:4631)
W/System.err:     at android.telephony.TelephonyManager.setDataEnabled(TelephonyManager.java:6453)
W/System.err:     at android.telephony.TelephonyManager.setDataEnabled(TelephonyManager.java:6430)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.example.app.MainActivity.toggleMobileNetwork(MainActivity.java:51)
W/System.err:     at com.example.app.MainActivity.disableMobileNetwork(MainActivity.java:41)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)

So, anybody has an idea what I have to do here?

toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

0

Okay, after doing some additional research I discovered an alternative solution here on SO which works like a charm.

Exactly what I needed: https://stackoverflow.com/a/50912113/1020704

toom
  • 12,864
  • 27
  • 89
  • 128