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

标签 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;
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),则该方法不可调用。

关于java - 启用/禁用数据问题 -java.lang.NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586350/

相关文章:

java - 如何在特定时间后将应用程序发送到后台

javascript - 使用 phonegap 创建移动聊天应用程序 - 最佳策略?

java - 捕获所有可能的路径参数 jax-rs regex

java - 在 Weblogic 12C 上访问 Spring Boot 示例应用程序的全新部署时出现问题

java - Spring Boot 应用程序可以在调试器中运行,但不能作为 jar 运行

android - SimpleCursorAdapter 替代品

java - java中类型转换时出错

Android:纹理的 gl 状态与着色器的不匹配

javascript - 动态加载的 phonegap.js 问题

c# - 移动 Azure 服务 C# .NET 后端补丁未更新