Trouble Getting BroadcastReceiver to work with AlarmManager

sadelbridsadelbrid USMember
edited February 2017 in Xamarin.Android

I'm creating an alarm with this code:

        Intent alarmIntent = new Intent(context, typeof(AlarmReceiver));
        notificationClickIntent = PendingIntent.GetActivity(context, 0, new Intent(), 0);
        pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
        am = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);

        DateTime setTime = new DateTime(temp.Ticks + offset); //temp is the current time where seconds field = 0
        if ((int)Build.VERSION.SdkInt >= 21) //my device enters this case
        {
            AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(setTime.Ticks, notificationClickIntent);
            am.SetAlarmClock(info, pendingIntent);
        }
        else {
            am.SetExact(AlarmType.RtcWakeup, setTime.Ticks, notificationClickIntent);
        }

And here is my receiver:

        [BroadcastReceiver (Process = ":remote")]
        public class AlarmReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                Console.WriteLine("alarm fired");
                Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
            }
        }

My receiver isn't receiving anything. Would this have something to do with my passing a tick value to AlarmClockInfo rather than milliseconds? Do I need to do a conversion? (I've tried that, it didn't help but maybe I did it wrong). Also worth noting, I've logged the datetime value and it is the correct time.

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    edited February 2017

    Hello @sadelbrid
    i'm not an expert but you need to register your receiver inside your manifest ...
    Do you have register your receiver ?

    have a nice day

  • sadelbridsadelbrid USMember

    @ChristopheBERNARD said:
    Hello @sadelbrid
    i'm not an expert but you need to register your receiver inside your manifest ...
    Do you have register your receiver ?

    have a nice day

    According to Docs and examples online, if you put

            [BroadcastReceiver]
    

    above the class and extend BroadcastReceiver, Xamarin will register it in the manifest when you compile your code.

  • criapixcriapix Member

    Hi.
    Process=":remote" is hard to debug cause it's running in a different thread.
    Breakpoints don't work.
    Try to use log messages to debug.

    []'s
    Patrick Coelho

Sign In or Register to comment.