启用/禁用数据问题-java.lang.NoSuchMethodException

启用/禁用数据问题-java.lang.NoSuchMethodException,java,android,mobile,Java,Android,Mobile,在下面链接的帮助下,我试图编写启用/禁用数据的代码 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; im

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

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());
    }
}
final Class conmanClass=Class.forName(GlobalService.getConnectivityService(context.getClass().getName());
最终字段iConnectivityManagerField=conmanClass.getDeclaredField(“MSService”);
iConnectivityManagerField.setAccessible(true);
最终对象iConnectivityManager=iConnectivityManagerField.get(GlobalService.getConnectivityService(context));
最终类iConnectivityManager类=Class.forName(iConnectivityManager.getClass().getName());
final方法[]methods=iConnectivityManagerClass.getDeclaredMethods();
用于(最终方法:方法){
if(method.toGenericString()包含(“set”)){
Log.i(“测试”,“方法:”+Method.getName());
}
}

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

这是因为该方法未导出,因此您无法调用它-即使是通过反射。我猜你在安卓L仿真器上遇到了上述错误,嗯?@Chungpham一个方法不被导出意味着什么?你能给我发电子邮件吗teqtic@gmail.com? 我真的很想找到一个解决办法。@Flyview:我将在这里发表我的评论,以便其他人可以阅读,而不是亲自将我的回复邮寄给你。当谷歌工程师构建安卓固件(如安卓L)版本时,该版本将导出公共方法(即
public
identifier)或私有/隐藏方法(即声明为
private
和/或标记为
@hide
identifier)。然后,第三方应用程序可以通过Refletion访问隐藏的方法。因此,如果某个方法未随固件一起导出,则该方法不存在。因此,使用反射调用此类方法将返回
NoSuchMethodException
。“就是这样。”Chungpham那么基本上,方法就不存在了!为什么它在grep源代码中?我们需要弄清楚ConnectivityManager现在是如何切换数据的。@Flyview:除非有人在Google上记录一个请求,要求导出
setMobileDataEnabled
方法,即使该方法是隐藏的,否则没有解决方案,所以我们可以使用反射来调用它。什么是GlobalService?再次询问:什么是GlobalService?什么是GlobalService?