我使用此代码在我的应用程序中启用或禁用Internet 3G数据。我读了几个关于隐藏反射功能的问题以及所有这些问题,但它在数千款具有非常不同的Android版本的手机中运行良好(我的应用程序在PlayStore中,我没有遇到任何问题)。但我很担心,因为我发现一个人的电话无法做到这一点。让我先展示一下我使用的代码:
try
{ final ConnectivityManager conman = (ConnectivityManager) MyContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if(conman == null) return false;
Class conmanClass = Class.forName(conman.getClass().getName());
if(conmanClass == null) return false;
Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
if(iConnectivityManagerField == null) return false;
iConnectivityManagerField.setAccessible(true);
Object iConnectivityManager = iConnectivityManagerField.get(conman);
if(iConnectivityManager == null) return false;
Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
if(iConnectivityManagerClass == null) return false;
Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
if(setMobileDataEnabledMethod == null) return false;
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, true/false); //Here is where you choose to enable or to disable
return true; //Everything went OK
}catch(Exception e)
{ return false;
}
错误符合:
Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
结果如下:
java.lang.NoSuchFieldException:mService
该手机是使用Jelly Bean 4.1.1的三星Galaxy SII 有任何想法吗?我害怕人们开始报道同样的问题。
答案 0 :(得分:1)
显然,三星(或可能是ROM mod作者)重写了该类,并且不再有名为mService
的数据成员。这完全在他们的权利范围内。只要他们的更改不会破坏CTS涵盖的任何内容,他们就可以通过框架类的内部实现来做他们想要的事情。这就是为什么我和其他Android专家告诉开发人员不要依赖于你正在使用的脚本小子技巧。
如果“启用或禁用互联网3G数据”对您的应用程序至关重要,并且您确定这会影响Samsung Galaxy SII上的库存ROM,则需要阻止通过Play商店向这些设备分发。
如果“启用或停用互联网3G数据”对您的应用并不重要,请添加一个更好的异常处理程序,说“对不起,此功能不可用”,或者某些此类功能。