在下面的链接的帮助下,我尝试编写用于启用/禁用数据的代码。

http://stackoverflow.com/questions/23100298/android-turn-on-off-mobile-data-using-code


但是在运行代码时,我得到了java.lang.NoSuchMethodException

主要代码:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import android.util.Log;


//the method below enables/disables mobile data depending on the Boolean 'enabled' parameter.
        public void setMobileDataEnabled(Context context, boolean enabled) {
            this.context = context;
            //create instance of connectivity manager and get system connectivity service
                final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                 try {
                    //create instance of class and get name of connectivity manager system service class
                     Class conmanClass = Class.forName(conman.getClass().getName());
                    //create instance of field and get mService Declared field
                    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
                  //Attempt to set the value of the accessible flag to true
                    iConnectivityManagerField.setAccessible(true);
                  //create instance of object and get the value of field conman
                    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
                    //create instance of class and get the name of iConnectivityManager field
                    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
                  //create instance of method and get declared method and type
                    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
                    //Attempt to set the value of the accessible flag to true
                    setMobileDataEnabledMethod.setAccessible(true);
                  //dynamically invoke the iConnectivityManager object according to your need (true/false)
                    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
                    //Thread.sleep(1000L);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                   Log.d("POST", "Method not found");
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        // below method returns true if mobile data is on and vice versa
         public boolean mobileDataEnabled(Context context){
                boolean mobileDataEnabled = false; // Assume disabled
                ConnectivityManager cm = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE);
                try {
                    Class cmClass = Class.forName(cm.getClass().getName());
                    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
                    method.setAccessible(true); // Make the method callable
                    // get the setting for "mobile data"
                    mobileDataEnabled = (Boolean)method.invoke(cm);
                } catch (Exception e) {
                    // Some problem accessible private API
                    // TODO do whatever error handling you want here
                }
                return mobileDataEnabled;
            }


无法找出我缺少的地方。有人可以向我指出代码中缺少的地方吗?

错误:

01-03 06:18:05.549: W/System.err(5663): java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
01-03 06:18:05.559: W/System.err(5663):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
01-03 06:18:05.559: W/System.err(5663):     at java.lang.Class.getDeclaredMethod(Class.java:640)
01-03 06:18:05.559: W/System.err(5663):     at Core.MobileDataNetwrokHandler.setMobileDataEnabled(MobileDataNetwrokHandler.java:129)
01-03 06:18:05.559: W/System.err(5663):     at com.test.post.MainActivity.mobileData(MainActivity.java:59)
01-03 06:18:05.559: W/System.err(5663):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 06:18:05.559: W/System.err(5663):     at java.lang.reflect.Method.invoke(Method.java:515)
01-03 06:18:05.559: W/System.err(5663):     at android.view.View$1.onClick(View.java:3818)
01-03 06:18:05.559: W/System.err(5663):     at android.view.View.performClick(View.java:4438)
01-03 06:18:05.559: W/System.err(5663):     at android.view.View$PerformClick.run(View.java:18439)

最佳答案

要查看ConnectivityManager类的方法是否可以通过反射调用,请尝试以下操作:

final Class<?> conmanClass = Class.forName(GlobalService.getConnectivityService(context).getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(GlobalService.getConnectivityService(context));
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method[] methods = iConnectivityManagerClass.getDeclaredMethods();
for (final Method method : methods) {
    if (method.toGenericString().contains("set")) {
        Log.i("TESTING", "Method: " + method.getName());
    }
}


如果该方法在LogCat输出中不存在(例如,setMobileDataEnabled),则该方法不可调用。

10-08 19:32