Call getMobileDataEnabled() and setMobileDataEnabled.
Something like this:
*setMobileDataEnabled(!getMobileDataEnabled());*
/**
* Gets the value of the setting for enabling Mobile data.
*
* @return Whether mobile data is enabled.
* @hide
*/
public boolean getMobileDataEnabled() {
try {
return mService.getMobileDataEnabled();
} catch (RemoteException e) {
return true;
}
}
/**
* Sets the persisted value for enabling/disabling Mobile data.
*
* @param enabled Whether the mobile data connection should be
* used or not.
* @hide
*/
public void setMobileDataEnabled(boolean enabled) {
try {
mService.setMobileDataEnabled(enabled);
} catch (RemoteException e) {
}
}
On Saturday, March 9, 2013 11:00:31 PM UTC-6, mohammed Nuhail wrote:
>
> Hello people,
> I want to enable and disable 3g programmatically on pressing single a
> button..
> below code only enable the 3g.
> data.setOnClickListener(new OnClickListener() {
>
> @Override
> public void onClick(View v) {
> // TODO Auto-generated method stub
> ConnectivityManager cm =
> (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
> //NetworkInfo ni = cm.getActiveNetworkInfo();
>
> try {
> Method dataMtd;
> dataMtd =
> ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled",
> boolean.class);
>
> Log.d("start", "ing");
> if(dataMtd.isAccessible()){
> dataMtd.setAccessible(false);
> dataMtd.invoke(cm, false);
> Log.d("data", "off");
> //return;
>
> }
> else {
> dataMtd.setAccessible(true);
> dataMtd.invoke(cm, true);
> Log.d("data", "on");
> }
>
>
> } catch (NoSuchMethodException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (IllegalArgumentException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (IllegalAccessException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (InvocationTargetException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> });
>
> I want to disable it, when it is enabled ?
>
> please help me out with this..
>
>
> thanks in advance
>
--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.