Request permission to use background data and battery management in Xamarin Android

Jcdv Del Vecchio 0 Reputation points
2023-10-21T09:13:19.9666667+00:00

In my Xamarin Android project I have to request permissions to use data in the background and not limit the use of the battery for background activities, the request works on many devices but on Oppo it doesn't work. I would like to allow foreground activities and background activities.

 Intent intentData = new Intent();  ConnectivityManager ConMgr = (ConnectivityManager)this.activity.GetSystemService(Context.ConnectivityService);  var pro = ConMgr.BackgroundDataSetting;  ConnectivityManager connMgr = (ConnectivityManager)this.activity.GetSystemService(Context.ConnectivityService);  string mess_titolo = Constants.ATTENZIONE;  string mess_mess = "Per la completa funzionalità dell’app è necessario consentire:\r\n\r\n 1. L'uso dati in background.\r\n" +      "2. L'uso dati con risparmio dati attivato.\r\n\r\nVerrete reindirizzati alle impostazione del telefono.";  switch (connMgr.RestrictBackgroundStatus)  {      case RestrictBackgroundStatus.Enabled:          if (await ShowDialogGiustificato(mess_titolo, mess_mess))          {              this.activity.StartActivity(new Intent(Android.Provider.Settings.ActionIgnoreBackgroundDataRestrictionsSettings, Android.Net.Uri.Parse("package:" + Android.App.Application.Context.PackageName)));           }          break;      case RestrictBackgroundStatus.Whitelisted:          Log.Debug(MTAG, ("Whitelisted").ToUpper());          if (await ShowDialogGiustificato(mess_titolo, mess_mess))          {              this.activity.StartActivity(new Intent(Android.Provider.Settings.ActionIgnoreBackgroundDataRestrictionsSettings, Android.Net.Uri.Parse("package:" + Android.App.Application.Context.PackageName)));           }          break;      case RestrictBackgroundStatus.Disabled:          Log.Debug(MTAG, ("RISPARMIO BATTERIA DISABILITATO").ToUpper());          if (await ShowDialogGiustificato(mess_titolo, mess_mess))          {              this.activity.StartActivity(new Intent(Android.Provider.Settings.ActionIgnoreBackgroundDataRestrictionsSettings, Android.Net.Uri.Parse("package:" + Android.App.Application.Context.PackageName)));           }          break;  }

This is the system I currently use and it works on many devices and if you don't have permission it takes you to the permissions setting, but on Oppo it doesn't happen. How can I get it to ask for permission or take me to the settings?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.