Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable data connection on Android 2.3

Tags:

android

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?

like image 245
Richard Ye Avatar asked Dec 21 '22 05:12

Richard Ye


1 Answers

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

like image 79
Riyaz Mohammed Ibrahim Avatar answered Dec 23 '22 18:12

Riyaz Mohammed Ibrahim