Enable & Disable Data Connection in Android Programmatically - Stack Overflow PDF
Enable & Disable Data Connection in Android Programmatically - Stack Overflow PDF
Stack Overflow
log in
39
gprs
invocationtargetexception
I want to enable/disable the data connection programmatically. I've used the following code:
void enableInternet(boolean yes)
{
ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method iMthd = null;
try {
iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
} catch (Exception e) {
}
iMthd.setAccessible(false);
if(yes)
{
try {
iMthd.invoke(iMgr, true);
Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
dataButton.setChecked(false);
Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();
It's working without any errors in the emulator but, I'm getting "InvocationTargetException" when I try to run it on a real device. I'm using API level 8 to
build the application.
share
JiTHiN
4,584 4 23 54
Asked
Jul 19 '12 at 6:50
dfeuer
20.3k 3 27 80
Edited
Jan 12 '15 at 5:10
What is the result of e.getMessage() on the InvocationTargetException? How about the enclosed exception? Greyson Jul 19 '12 at 6:55
What device are you running this on and what version of Android OS is installed on that device. David Wasser Jul 19 '12 at 7:19
I'm running it on a Samsung Galaxy S and the Android OS is 2.2 JiTHiN Jul 19 '12 at 8:16
add a comment
2 Answers
77
Order By
Votes
This code sample should work for android phones running gingerbread and higher:
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuch
MethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
}
setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
share
Answered
Jul 19 '12 at 6:57
msysmilu
769 6 11
Edited
Jan 6 '15 at 14:32
Is there any way to make it work on Froyo? JiTHiN Jul 19 '12 at 7:18
this doesn't work with 2.2 so what should we do for that.. Sumit Jul 28 '12 at 11:34
what parameter need to send data enable and data disable? Bucks Sep 5 '12 at 10:59
I got java.lang.reflect.InvocationTargetException error after run this code. mcacorner Jan 9 '13 at 11:36
For android versions less than 2.3 we can use the java reflection method to enable/disable data connection. Refer stackoverflow.com/questions/11662978/. JiTHiN Apr 23 '13
at 10:59
show 16 more comments
@riHaN JiTHiN your program works fine for 2.3 and above, But it needs a small change in 'else' statement:
else
{
try {
iMthd.invoke(iMgr, true);
share
Answered
Apr 20 '14 at 11:19
chiastic-security
14.4k 3 17 43
Edited
Sep 30 '14 at 14:35
Your Answer
log in
or
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app