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