I have a ToggleButton named gprs. I need it to turn on and off the gprs. How to accomplish that? I've looked here but it gives errros and I can't figure it out how to use it in my case.
Asked
Active
Viewed 827 times
1
-
What errors are you getting? – 323go Mar 13 '13 at 03:06
-
did you add that permission? – Nezam Mar 13 '13 at 04:04
-
check this http://stackoverflow.com/questions/11555366/enable-disable-data-connection-in-android-programmatically – Piyush Mar 13 '13 at 04:24
-
Check this http://stackoverflow.com/questions/11662978/how-to-provide-option-to-select-wi-fi-or-gprs-for-network-connectivity-in-androi also – JiTHiN Mar 13 '13 at 04:27
-
Nezam, yes I did. @PAD I saw this, but I don't how to implement it to my code. I have a toggle button – Nabukodonosor Mar 13 '13 at 11:49
1 Answers
0
OK, I will post here the solution if anyone has the same problem, using toggle button. First I created separated class for gprs settings:
public class GprsSettings {
static void setMobileDataEnabled(Context context, boolean enabled) {
try {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
Log.i("setMobileDataEnabled()","OK");
}
catch (Exception e)
{
e.printStackTrace();
Log.i("setMobileDataEnabled()","FAIL");
}
}
}
Then, first in my activity add some code to check if the gprs is on or off....place it above your onCreate method:
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
}
Then, in my activity I used this code for toggle button with toast:
gprs.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
if (((ToggleButton)v).isChecked()) {
GprsSettings.setMobileDataEnabled(getApplicationContext(), true);
Toast.makeText(getApplicationContext(), "GPRS is ON", Toast.LENGTH_LONG).show();
}else{
GprsSettings.setMobileDataEnabled(getApplicationContext(), false);
Toast.makeText(getApplicationContext(), "GPRS is OFF", Toast.LENGTH_LONG).show();
}
}
catch (Exception localException) {
Log.e("SwarmPopup", "error on GPRS listerner: " + localException.getMessage(), localException);
}
}
});
gprs.setChecked(isNetworkConnected());
That's it, works like a charm.

sparkhee93
- 1,381
- 3
- 21
- 30

Nabukodonosor
- 699
- 1
- 8
- 20
-
Actually it does not work like a charm. It works, but...the gprs button is on even if gprs on my phone is off but wireless is on. :(( – Nabukodonosor Mar 13 '13 at 16:22