2

I search a looooooot but I didn't find what I want.

I'm trying to make an app that the user are able to turn on and turn off by pressing a button for each one.

I found some codes using the reflection (I'll use the android 2.3 and up) but I don't know how to adapt it to use with buttons.

I search how reflection works, I spend a lot of time looking for other ways to do it but... anyone works for me.

The worst problem is the codes and examples I found didn't use the exceptions (throws or try catch) so I don't know how to work with them.

This is the code I found here, but I don't know how to use it (or call it as true of false, in order to Enable and Disable the 3G/Mobile data) in a Button click.

Anyone can help me?

private void setMobileDataEnabled(Context context, boolean enabled) {
        final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(conman);
        final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
     }

Link to where I found this code: How can i turn off 3G/Data programmatically on Android?

Community
  • 1
  • 1
Murillo Ferreira
  • 1,423
  • 1
  • 16
  • 31

1 Answers1

2

hi! you just have to call the function inside the onClickListener of each button and let the function run in a separate thread!

On Button:

btOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(MainActivity.this,"Trying to switch on 3G!",Toast.LENGTH_SHORT).show();

                (new Thread(new Runnable() {
                    @Override
                    public void run() {

                        setMobileDataEnabled(MainActivity.this,true);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "Switched ON 3G!", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                })).start();
            }
        });

Off button:

        btOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Trying to switch off 3G!",Toast.LENGTH_SHORT).show();

                (new Thread(new Runnable() {
                    @Override
                    public void run() {

                        setMobileDataEnabled(MainActivity.this,false);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "Switched OFF 3G!", Toast.LENGTH_SHORT).show();
                            }
                        });


                    }
                })).start();



            }
        });

* Add try...catch clauses to the function:

final ConnectivityManager conman;

conman = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    final Class conmanClass;

    try {


        conmanClass = Class.forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(conman);
        final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);

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

}

I hope that answers your question!

rps_deepan
  • 716
  • 4
  • 5
  • It is exactly what I was looking for, works perfectly, I really appreciate your help, I can't believe the only thing I needed to make was: setMobileDataEnabled(MainActivity.this, true/false). Thanks for your help (y) – Murillo Ferreira Jul 10 '13 at 05:29