-
-
Save gaecom/91cb1835dc2f400d0b03aee230eb4251 to your computer and use it in GitHub Desktop.
Programmatically on off mobile data on android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| boolean DataOnOff(boolean status, Context context) { | |
| int bv = 0; | |
| try { | |
| if (bv == Build.VERSION_CODES.FROYO){ | |
| //android 2.2 versiyonu için | |
| Method dataConnSwitchmethod; | |
| Class<?> telephonyManagerClass; | |
| Object ITelephonyStub; | |
| Class<?> ITelephonyClass; | |
| TelephonyManager telephonyManager = (TelephonyManager) context | |
| .getSystemService(Context.TELEPHONY_SERVICE); | |
| telephonyManagerClass = Class.forName(telephonyManager | |
| .getClass().getName()); | |
| Method getITelephonyMethod = telephonyManagerClass | |
| .getDeclaredMethod("getITelephony"); | |
| getITelephonyMethod.setAccessible(true); | |
| ITelephonyStub = getITelephonyMethod.invoke(telephonyManager); | |
| ITelephonyClass = Class.forName(ITelephonyStub.getClass() | |
| .getName()); | |
| if (status) { | |
| dataConnSwitchmethod = ITelephonyClass | |
| .getDeclaredMethod("enableDataConnectivity"); | |
| } else { | |
| dataConnSwitchmethod = ITelephonyClass | |
| .getDeclaredMethod("disableDataConnectivity"); | |
| } | |
| dataConnSwitchmethod.setAccessible(true); | |
| dataConnSwitchmethod.invoke(ITelephonyStub); | |
| } else { | |
| // android 2.2 üstü versiyonlar için | |
| final ConnectivityManager conman = (ConnectivityManager) context | |
| .getSystemService(Context.CONNECTIVITY_SERVICE); | |
| final Class<?> conmanClass = Class.forName(conman.getClass() | |
| .getName()); | |
| final Field iConnectivityManagerField = conmanClass | |
| .getDeclaredField("mService"); | |
| iConnectivityManagerField.setAccessible(true); | |
| final Object iConnectivityManager = iConnectivityManagerField | |
| .get(conman); | |
| final Class<?> iConnectivityManagerClass = Class | |
| .forName(iConnectivityManager.getClass().getName()); | |
| final Method setMobileDataEnabledMethod = iConnectivityManagerClass | |
| .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); | |
| setMobileDataEnabledMethod.setAccessible(true); | |
| setMobileDataEnabledMethod.invoke(iConnectivityManager, status); | |
| } | |
| return true; | |
| } catch (Exception e) { | |
| Log.e("Mobile Data", "error turning on/off data"); | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment