简体   繁体   中英

Xamarin.Android - Start service on Boot Completed

I am trying to start a service when my device boots up, but the service never starts.

I have added:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

to AndroidManifest.xml. My BroadcastReceiver looks like this:

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var start = new Intent(context, typeof(AlertSyncService));
        start.AddFlags(ActivityFlags.NewTask);
        context.StartService(start);
    }
}

I have confirmed my service is not the issue (tested by starting the same service on button click - it works fine). The issue is definitely that the BroadcastReceiver is never receiving the event. I've also ensured that I open the app once before rebooting - I saw in a similar question that this is necessary, as apps are installed in a "stopped state".

Does anybody have any ideas what could be causing this not to work? I'm relatively new to this, so highly likely I've missed something obvious!

Thanks

You could try this:

[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
[IntentFilter(new string[] {
    Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted, "android.intent.action.QUICKBOOT_POWERON"
})]
public class BootReceiver: BroadcastReceiver {
    public override void OnReceive(Context context, Intent intent) {
        //Do something
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM