You are on page 1of 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

39

Enable/disable data connection in android programmatically


android

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

improve this question

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

Dont forget to add this line to your manifest file


<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

share

improve this answer

Tobias Moe Thorstensen


3,809 4 43 92

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

the 'true' should be changed to 'false'


iMthd.invoke(iMgr, false);

share

improve this answer


ashish28ranjan
11 1

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

Email

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

Post Your Answer

2016 Stack Exchange, Inc

You might also like