Referring to How to disable Mobile Data on Android , we know the approach to enable/disable data connection in Android 2.2 by java reflection.
However, in Android 2.3 and up, android.permission.MODIFY_PHONE_STATE
is no longer supported and I found the way above does not work in Android 2.3. Do you have another idea to enable data connection?
Asked
Active
Viewed 7,650 times
4

Community
- 1
- 1

Richard Ye
- 191
- 1
- 5
- 12
3 Answers
9
ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(true);
dataMtd.invoke(mgr, true/false);
you need android.permission.CHANGE_NETWORK_STATE permission too

Riyaz Mohammed Ibrahim
- 9,505
- 5
- 26
- 33
-
Hi, Thanks. The only issue is that it cannot stably enable data connection in my HTC EVO 4G -- although the widget seems enabled but it is not really data connected. (Disabling data connection is always okay) – Richard Ye Dec 05 '11 at 07:03
-
I get following runtime error java.lang.reflect.InvocationTargetException – mcacorner Jan 09 '13 at 11:27
0
In your androidmanifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
;)