1

I wrote a broadcast receiver in Xamarin.Android which seams perfect, but I don't know why it doesn't get called when device boots, can someone please just tell me what is wrong with my code ?

[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action.Equals("android.intent.action.BOOT_COMPLETED"))
        {
            Toast.MakeText(context, "1 Received intent! You can run your background task here.", ToastLength.Short).Show();
        }
    }
}

I wrote the broadcast receiver as shown above, but it doesn't get launched on boot.

John Code
  • 655
  • 7
  • 24

1 Answers1

1

You do not have to explicitly set Exported = "true" since you already have at least one intent filter so it is redundant. Your permission is also missing from your receiver, which is required. Don't manually edit your AndroidManifest.xml because it will be overwritten by Xamarin.

You also have to configure any battery optimization service that your mobile phone is running example Huawei. You have to disable battery optimization for this app otherwise it will not receive any broadcast.

After installing the application, you have to open it from the icon at least one time, otherwise it will be delivered any broadcast by the Android System.

Sample Code:

namespace AZ.Sample.Notifications
{
    using System;
    using Android;
    using Android.App;
    using Android.Content;

    [BroadcastReceiver(Enabled = true, Permission = Manifest.Permission.ReceiveBootCompleted)]
    [IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority, Categories = new[] { Intent.CategoryDefault })]

    public class BootBroadcastReceiver : BroadcastReceiver
    {

        public override void OnReceive(Context context, Intent intent)
        {

            Toast.MakeText(Android.App.Application.Context, "Broadcast Received", ToastLength.Long).Show();

            var uri = Android.Net.Uri.Parse("http://www.google.com");
            var intent1 = new Intent(Intent.ActionView, uri);
            intent1.AddFlags(ActivityFlags.NewTask);

            intent1.SetPackage("com.android.chrome");
            try
            {
                context.StartActivity(intent1);
            }
            catch (ActivityNotFoundException ex)
            {
                ex.PrintStackTrace();
                //Chrome browser not installed
                intent.SetPackage(null);
                context.StartActivity(intent1);
            }   

        }
    }
}

Chrome should be installed on the mobile device on which you will run this sample code, after restarting the device don't let it sleep and wait at least 2 minutes. After that it will show a toast message and also open a website in Chrome browser.

AZ_
  • 21,688
  • 25
  • 143
  • 191
  • tell me how to start the class? i try this. ```var intent1 = new Intent(context, typeof(BootRestoreAlarm)); intent1.SetPackage("app.test.testapp"); context.StartService(intent);``` But it don`t work. In BootRestoreAlarm class i work width Local BaseData and Local Notifaction. – alexandr f Dec 22 '21 at 08:06