java.lang.NoSuchMethodException:Lollipop 上的 getTetherableIfaces

java.lang.NoSuchMethodException: getTetherableIfaces on Lollipop

我的应用程序需要检查是否有可用的 USB 可连接接口。为此,它使用反射在 ConnectivityManager 上调用 getTetherableIfaces。

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getTetherableIfaces");
method.setAccessible(true);
method.invoke(cm, args); 

我已经在 LG Leon 运行 Android 5.0.1 上测试过,但失败并显示 java.lang.NoSuchMethodException。

Lollipop 中是否删除或更改了此功能?

根据http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.1_r1/android/net/ConnectivityManager.java#ConnectivityManager.getTetherableIfaces%28%29方法仍然存在。

也许你应该试试 ConnectivityManager.class.getDeclaredMethod("getTetherableIfaces")

您是否尝试查看连接管理器的所有方法列表?你能给我们看看下面代码的日志吗?

Method[] methodArray = ConnectivityManager.class.getMethods();
for (Method method : methodArray) {
   Log.v(TAG, method.getName());
}