سؤال

I use this code for enabling or disabling Internet 3G Data in my application. I read several questions of it about hidden reflection functions and all that, but it was working very well in thousands of phones with very different android releases (my application is in PlayStore and I had no problems with it). But I am worried because I found a person whose phone is not able to do this. Let me first show the code I use:

 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;
 }

The error is in line:

Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");

With this result:

java.lang.NoSuchFieldException: mService

That phone is a Samsung Galaxy SII using Jelly Bean 4.1.1 Any ideas? I am afraid of people starting to report same issue.

هل كانت مفيدة؟

المحلول

Apparently, Samsung (or possibly a ROM mod author) rewrote that class and no longer has a data member named mService. This is completely within their rights. So long as their change does not break anything covered by the CTS, they can do what they want with the internal implementations of framework classes. This is why I and other Android experts tell developers not to rely on script-kiddie tricks like what you are using.

If "enabling or disabling Internet 3G Data" is crucial to your app, and you determine that this is affecting stock ROMs on the Samsung Galaxy SII, you will need to block distribution to such devices through the Play Store.

If "enabling or disabling Internet 3G Data" is not crucial to your app, add in a better exception handler to say "sorry, this feature is not available", or some such.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top