1

I want turn on or off Mobile Data by pressing a button On or button Off .I have written this code for setOnClickListner and Given Permission in Manifest File.But i am not able to turn on Mobile Data.I just want to turn mobile Data On without any configuration.My android Os version is Marshmellow.

  btnDataOn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                final ConnectivityManager conman = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                final Class conmanClass = Class.forName(conman.getClass().getName());
                final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
                connectivityManagerField.setAccessible(true);
                final Object connectivityManager = connectivityManagerField.get(conman);
                final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
                final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
                setMobileDataEnabledMethod.setAccessible(true);
                setMobileDataEnabledMethod.invoke(connectivityManager, true); } catch (NoSuchMethodException e1) {
                e1.printStackTrace();

            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (NoSuchFieldException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            }

        }
    });

Following is the Manifest File:-

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
  • 2
    Possible duplicate of [The setMobileDataEnabled method is no longer callable as of Android L and later](https://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later) – Bö macht Blau Apr 13 '18 at 17:23
  • What is exception? – Khemraj Sharma Apr 13 '18 at 18:00

1 Answers1

0
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
    connectivityManagerField.setAccessible(true);
    final Object connectivityManager = connectivityManagerField.get(conman);
    final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}

check this permission to your manifest class. and ask permission at run time link if needed.

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • What is difference in my code and your code...and what is the code needed for runtime ask permission.Please give one code snippet for above requirement. – SANKET DARWANTE Apr 14 '18 at 03:44