BroadcastReceiver - Can't Start Service on Boot | Reboot

frakafraka ✭✭TZMember ✭✭

Hello everyone,

I am facing problem to start background service on boot. My service runs without problems if started from anywhere else, but on boot, crashes immediately. Below is my BroadcastReceiver class;

namespace WSTests.Droid
{

    [BroadcastReceiver(Enabled = true)]
    public class BootComplete : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            try
            {
                if (intent.Action.Equals(Intent.ActionBootCompleted))
                {
                    Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();
                    Intent i = new Intent(context, typeof(SimpleService));
                    i.AddFlags(ActivityFlags.NewTask);
                    context.StartActivity(i);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

I have attached sample project as well for more details. I will appreciate for some help.

Tagged:

Best Answers

Answers

  • DarioOrgazDarioOrgaz ✭✭ USMember ✭✭

    Where you try to run on boot exactly?

  • frakafraka ✭✭ TZMember ✭✭

    I try to run on a real device.

  • DooksDooks ✭✭✭ ZAMember ✭✭✭
    edited February 2018

    I have created a Github repository Android-Job-XamarinBinding

    Compile it in release mode, and include the DLL in your project. Use JobManager as described on the Evernote repository here

    Your job wil restart when the device restarts, it will also work on Android 'O' Oreo.

    Remove your Alarm Receiver, Boot Receiver, Connectivity Receiver and change your service to IntentService and only use intents to that service within your app whilst open. Do all your background work in the Job.

  • frakafraka ✭✭ TZMember ✭✭

    @Dooks thanks so much -- it worked like a charm. Few settings on the broadcaster did the trick.

    Your contribution has been appreciated.

Sign In or Register to comment.