0

I have a PreferenceActivity in which there is a CheckboxPreference. I would like clicking on CheckboxPreference enable or disable Mobile Data, so I wrote this code.

//Outside of Methods
void turnData(boolean ON) throws Exception
    {

       final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
       final Class conmanClass = Class.forName(conman.getClass().getName());
       final java.lang.reflect.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, ON);
    }

    //In the onCreate
    final boolean o = false;
    boolean mobileDataEnabled = false;
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        try {
            Class cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean)method.invoke(cm);
        } catch (Exception e) {

        }

        if(mobileDataEnabled) {
            ((CheckBoxPreference)Data).setChecked(true);

            //The Mobile Data are enabled
            Data.setSummary("Mobile Data enabled");
            dati = true;
        }

        else {
            //The Mobile Data are disabled
            ((CheckBoxPreference)Data).setChecked(false);

            try {
                turnData(mobileDataEnabled);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Data.setSummary("Mobile Data disabled");
            dati = false;
        }

        Data.setOnPreferenceClickListener( new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {

                 if (dati==true) {

                        Data.setSummary("Mobile Data enabled");
                        dati = false;

                 }

                 else if (dati==false){

                     try {
                            //The problem is here
                            //What should I put in the parentheses?
                            turnData(o);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        Data.setSummary("Mobile Data disabled");
                        dati = true;

                 }

                return false;


            }
            });

The problem, as noted in the comments in the code, is what to put in the parentheses when calling the method. I apologize if the question may seem trivial **but please bear with me. I'm beginner.

Mario G.
  • 393
  • 1
  • 3
  • 16
  • http://stackoverflow.com/questions/11662978/how-to-provide-option-to-select-wi-fi-or-gprs-for-network-connectivity-in-androi/11663173#11663173 – JiTHiN Nov 25 '13 at 16:32
  • Put code that enabling/disabling mobile data into method and call that method in `onPreferenceClick` method. – Simon Dorociak Nov 25 '13 at 16:36
  • Hello, thanks for responding. I've updated my question and i tried to do as suggested but i've a problem. Can i help me? Thanks in advance. – Mario G. Nov 25 '13 at 16:53

1 Answers1

0

If I understood correctly, you can't call the code because, as you said,

"when i Click on the CheckBoxPreference, nothing happens."

Did you try binding the checkbox to the Preference.OnPreferenceChangeListener? The default "Preference Activity template" shows how to do this. Check this out: https://stackoverflow.com/questions/13596250/how-to-listen-for-preference-changes-within-a-preferencefragment

private OnPreferenceChangeListener onPrefChangeListener = new OnPreferenceChangeListener() {

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        return false;
    }
};
Community
  • 1
  • 1
EdGs
  • 356
  • 6
  • 18