启用和禁用移动数据

时间:2013-11-25 17:07:30

标签: java android xml

再次提出我的问题。对不起,重复。

我有一个PreferenceActivity,其中有一个CheckboxPreference。我想点击CheckboxPreference启用或禁用移动数据,所以我写了这段代码。

//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;


            }
            });

问题,如代码中的注释所述,是在调用方法时放在括号中的内容。如果这个问题看似微不足道,我会道歉**但请耐心等待。我是初学者。

0 个答案:

没有答案