locked
How can I run a service from the Boot? RRS feed

  • Question

  • User388951 posted

    I need to run a service when the phone is Off and then you turn it On

    Thursday, November 12, 2020 1:04 PM

All replies

  • User382871 posted

    Try using a BroadcastReceiver to start the service at startup. Set ActionBootCompleted action for the receiver class. [BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)] [IntentFilter(new string[] {Intent.ActionBootCompleted})] public class CustomReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { //Do something } }

    Don't forget to add the RECEIVEBOOTCOMPLETED permission in the AndroidManifest.xml.

    Thursday, November 12, 2020 2:19 PM
  • User388951 posted

    @Jarvan And where I have to instance the class CustomReceiver to use it?

    Friday, November 20, 2020 2:38 PM
  • User382871 posted

    @SpontonLuciano said: @Jarvan And where I have to instance the class CustomReceiver to use it?

    You could start the receiver in the MainActivity class or using the DependencyService to start it in the shared project.

    Monday, November 23, 2020 1:46 AM
  • User382871 posted

    @SpontonLuciano Hi, any updates? If the solution helps you to solve the issue, please accept the helpful reply as the answer. It'll help others who face the similar problem. If you are facing some issues while implementing, try to post the particular error with the corresponding codes here.


    Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.

    Monday, November 30, 2020 9:41 AM