0

I have a BroadcastReceiver that it will start a foreground service when the device reboots.

This is the code:

[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class ActionBootCompletedBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == Intent.ActionBootCompleted)
        {
            ServicioConfiguracion miServicioConfiguracion = new ServicioConfiguracion();

            bool miBlMyParamter = miServicioConfiguracion.GetParameter();

            if (miBlMyParameter == true)
            {
                var foreGroundServiceIntent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
                Android.App.Application.Context.StartForegroundService(intent);
                context.StartForegroundService(foreGroundServiceIntent);
            }
        }
    }
}

And this is the code of the configuration manager that reads the value of configuration of the application:

    public bool GetParameter()
    {
        return Preferences.Default.Get<bool>("MyParameter", false);
    }

The problem is that it seems that the BroadCastReceiver can't get the value of the parameter, because if I comment the if that determinates if start the foreground service or not, it starts.

The other problem that I have it is I don't know how to debug the BroadcastReceiver, because when the device reboots, the debugger stops.

My idea is that the user can configure in the application if starts the foreground service or not when the device starts.

How could I get the values of configuration of the application in the BroadCastReceiver?

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • Try trigger the broadcast manually: ``adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p yourPackageName ``. Add Persmissions in Andriod: ```` – Amjad S. May 14 '23 at 17:22

1 Answers1

1

I used the Toast to show the result:

    [BroadcastReceiver(Exported = true)]
    [IntentFilter(new[] {Intent.ActionBootCompleted})]
    public class MyReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            var a = Preferences.Default.Get<bool>("Test", false);
            Toast.MakeText(context, a.ToString(), ToastLength.Long).Show();
        }
    }

And set the value in the MainActivity:

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Preferences.Default.Set<bool>("Test", true);    
    }

The result image:

enter image description here

Update1:

I used the adb in the visual studio successfully. First of all, click Tools->Android->Android Adb Command Prompt to open the adb command panel.

And then use the adb root command to get into root mode. After this, I can send the boot completed broadcast.

enter image description here

In addition, if you want to start foreground service in the background(after the device boot completed), you can add the following permission in the AndroidManifest.xaml:

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
      <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
      <uses-permission android:name="android.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND" />

I can start the foreground service in the OnReceive method when I restarted the emulator android api 33.

And I checked your code in the OnReceive method: Android.App.Application.Context.StartForegroundService(intent); this line should be deleted. The intent is the parameter of the OnReceive method. Your foreground service intent is foreGroundServiceIntent.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Yes, i get this exception when I try the broadcast: not allowed to send broadcast android.intent.action.BOOT_COMPLETED. But I can't see in my case the toast message, so I guess I don't cofigure correctly the broadcast receiver. – Álvaro García May 16 '23 at 18:19
  • about the command, it seems one solution is to start adb in root mode: https://stackoverflow.com/questions/40698450/android-trying-to-test-a-service-on-boot-java-lang-securityexception-permiss But I can't start adb in root mode. I get an error. – Álvaro García May 16 '23 at 18:23
  • Please check the update part in my answer. @ÁlvaroGarcía – Liyun Zhang - MSFT May 17 '23 at 03:03