2

I am trying to switch on mobile data or wifi from a popup in my application but unable to switch on mobile data. I trying this by using the following code...

public void TurnOnInternet()
   {
     AlertDialog.Builder alert = new AlertDialog.Builder(_context);
         alert.SetTitle(Resource.String.networktitle);
        alert.SetMessage("Select the etwork you Like to Enable from the following");
      alert.SetPositiveButton("Enable Wifi", (senderAlert, args) =>
           {
             Intent tntent = new Intent(Android.Provider.Settings.ActionSettings);
                _context.StartActivity(tntent);
              _context.Finish();
              WifiManager wifi = (WifiManager)GetSystemService(Context.WifiService);
               wifi.SetWifiEnabled(true);

               _context.Finish();
            });
           alert.SetNegativeButton("Enable Mobile Date", (senderAlert, args) =>
          {
       setMobileDataState();
       try
        {
          TelephonyManager telephonyService = (TelephonyManager)GetSystemService(Context.TelephonyService);
           Method setMobileDataEnabledMethod = telephonyService.Class.GetDeclaredMethod("getDataEnabled");
           if (null != setMobileDataEnabledMethod)
           {
            setMobileDataEnabledMethod.Invoke(telephonyService, true);
                      setMobileDataEnabledMethod.  .SetDataEnabled(true);
                    }
                _context.Finish();
               }
               catch (Exception ex)
                { }
            });

            Dialog dialog = alert.Create();
            dialog.Show();
        }
public void setMobileDataState()
        {
            try
            {
                Class  ITelephonyClass;
                TelephonyManager telephonyService = (TelephonyManager)GetSystemService(Context.TelephonyService);
                Method setMobileDataEnabledMethod = telephonyService.Class.GetDeclaredMethod("getDataEnabled");
                ITelephonyStub = setMobileDataEnabledMethod.Invoke(telephonyService);
                ITelephonyClass = Class.ForName(ITelephonyStub.Class.Name);

                if (null != setMobileDataEnabledMethod)
                {
                    Method dataConnSwitchmethod = ITelephonyClass.GetDeclaredMethod("enableDataConnectivity");

                    setMobileDataEnabledMethod.Invoke(telephonyService, true);
                 setMobileDataEnabledMethod.  .SetDataEnabled(true);
                    telephonyService.SetDataEnabled(true);
                    setMobileDataEnabledMethod.SetDataState(true);
                }
                _context.Finish();
            }
            catch (Java.Lang.Exception ex)
            { }

        }

So this is the code I was using but unable to get the work done. The mobile data portion is completely not working so please help me to turn on the mobile data in from my app itself without navigating to settings

If you know android also please let me know the solution so that i can get the code converted

ROHIT MANDALAPU
  • 428
  • 1
  • 5
  • 18

1 Answers1

0

I am trying to switch on mobile data or wifi from a popup in my application but unable to switch on mobile data.

I think mobile data and wifi are different things. For set Wifi state, it is simple, you can code for example like this:

public void TurnOnInternet(bool enabled)
{
    var wm = this.GetSystemService(Context.WifiService) as WifiManager;
    if (enabled)
    {
        if (!wm.IsWifiEnabled)
            wm.SetWifiEnabled(true);
    }
    else
    {
        if (wm.IsWifiEnabled)
            wm.SetWifiEnabled(false);
    }
}

And for this action, you will need the following permission in your manifest:

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

For mobile data, first of all, enabling/ disabling mobile network can only be done with root access, make sure your device supports it. Then from Android version 5.0, you may check the answer here, and for other versions, you can follow this answer.

The permissions for this work you may need are as following:

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

Cause I don't have a rooted phone for testing, although I created a demo, I'm not sure it works, so I won't post the related code here.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45