【发布时间】:2014-07-05 12:18:03
【问题描述】:
在以下链接的帮助下,我尝试编写启用/禁用数据的代码。
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)
【问题讨论】:
-
那是因为该方法没有被导出,因此你不能调用它——即使是通过反射。我猜你在使用 Android L 模拟器时遇到了上述错误吧?
-
@ChuongPham 不导出方法是什么意思?你能给我发电子邮件到 teqtic@gmail.com 吗?我真的很想找到一个解决方案。
-
@Flyview:我不会亲自将我的回复邮寄给您,而是将我的评论发布在这里,以便其他人可以阅读。当 Google 工程师构建 Android 固件版本(例如 Android L)时,该版本将导出公共方法(即
public标识符)或私有/隐藏方法(即声明为private和/或标记为@987654327 @标识符)。然后,第三方应用程序可以通过反射访问隐藏的方法。 所以,如果一个方法没有随固件导出,则它不存在。 所以,使用反射调用该方法将返回NoSuchMethodException。就是这样。 -
@ChuongPham 那么基本上,方法不存在!为什么它在 grep 代码源中?我们现在需要弄清楚 ConnectivityManager 是如何切换数据的。
-
@Flyview:除非有人向 Google 发出请求以导出
setMobileDataEnabled方法,即使它是隐藏的,否则没有解决方案,因此我们可以使用反射来调用它。