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

2 answers

Sort by: Most helpful
  1. Sheeraz Ali 170 Reputation points
    2025-09-25T06:39:57.8933333+00:00

    These vendors use ColorOS, which adds aggressive power and background restrictions on top of standard Android. That’s why your current ActionIgnoreBackgroundDataRestrictionsSettings intent works on Samsung/Pixel/etc. but not on Oppo

    Unfortunately, there’s no universal API for Oppo’s proprietary settings. You need to:

    Handle standard Android APIs (what you already do).

    Detect if the device is Oppo/Realme (ColorOS).

    1. Redirect to OEM-specific settings screens via custom intents.

    You can detect the brand with:

    string manufacturer = Android.OS.Build.Manufacturer?.ToLower();
    if (manufacturer.Contains("oppo") || manufacturer.Contains("realme"))
    {
        try
        {
            // Try to open Oppo "Auto Start" settings
            Intent intent = new Intent();
            intent.SetComponent(new ComponentName(
                "com.coloros.safecenter",
                "com.coloros.safecenter.permission.startup.StartupAppListActivity"
            ));
            activity.StartActivity(intent);
        }
        catch (Exception)
        {
            try
            {
                // Fallback for some Oppo/Realme models
                Intent intent = new Intent();
                intent.SetComponent(new ComponentName(
                    "com.coloros.safecenter",
                    "com.coloros.safecenter.startupapp.StartupAppListActivity"
                ));
                activity.StartActivity(intent);
            }
            catch (Exception)
            {
                // Final fallback – open general settings
                Intent intent = new Intent(Android.Provider.Settings.ActionSettings);
                activity.StartActivity(intent);
            }
        }
    }
    else
    {
        // Default Android handling
        activity.StartActivity(new Intent(
            Android.Provider.Settings.ActionIgnoreBackgroundDataRestrictionsSettings,
            Android.Net.Uri.Parse("package:" + activity.PackageName)
        ));
    }
    
    

  2. Jcdv Del Vecchio 0 Reputation points
    2025-09-25T15:16:54.7933333+00:00

    Thanks so much for your reply, I'll try the changes as soon as I can.

    0 comments No comments

Your answer

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