I tired it on Android 2.3 i tried this code and it worked! When I looked into Android source code I saw this method is available 2.2 R1 up.
private boolean getMobileDataEnabled() {
try {
Method method = connectivityManager.getClass().getMethod("getMobileDataEnabled");
return (Boolean)method.invoke(connectivityManager);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private void setMobileDataEnabled(boolean on) {
try {
Method method = connectivityManager.getClass().getMethod("setMobileDataEnabled",
boolean.class);
method.invoke(connectivityManager, on);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
If you are using 2.2 R1 or later try this code.
Good catch. Thanks for sharing
ReplyDeletehi could u share your full code of that?
ReplyDeletei have tried it in my emulator and it gave me java.lang.reflect.IncocationTargetException.
please reply my comment. thx
thanks for sharing , i want this code in my inbox : java.lok2011@gmail.com
ReplyDeleteMethod method = connectivityManager.getClass().getMethod("setMobileDataEnabled", boolean.class);
ReplyDelete=========================
what is connectivityManager
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
DeleteThis comment has been removed by the author.
ReplyDeleteSorry, doesn't work on 2.3
ReplyDeleteI used this code.
ReplyDeleteI got exception on method invoke