Forum Xamarin.Forms

IntentFilter for "ALARMMANAGER_TRIGGERED" action?

MAVZoliMAVZoli Member ✭✭

See the question, i want to filter intents in my Broadcast receiver, just like i do for "BOOT_COMPLETED" action with "Intent.ActionBootCompleted".

Thank you!

Best Answer

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited February 21 Accepted Answer

    Here are code for broadcast receiver for system alarm event: `ALARMMANAGER_TRIGGERED

     [BroadcastReceiver]
     [IntentFilter (new[] { AlarmReceiver.ALARM_TRIGGER })]
     public class AlarmReceiver : BroadcastReceiver
     {
      public const string ALARM_TRIGGER = "ALARM_TRIGGERED";
    
      public override void OnReceive(Context context, Intent intent) 
      {
    
        Intent startIntent = new Intent(context, typeof(AlarmIntentService));
        context.StartService(startIntent);
      }
      }
    

    Code for broadcast receiver for system boot-completed event:

       [BroadcastReceiver]
       [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted } )]
       public class BootReceiver : BroadcastReceiver
       {
          public override void OnReceive (Context context, Intent intent)
       {
    
            Intent startIntent = new Intent (context, typeof(BootIntentService));
            context.StartService (startIntent);
       }
       }
    

    Add to AndroidManifest.xml:

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

    If you use Xamarin forms, you can use DependencyService to register BroadcastReceiver.
    Create a interface.

        public  interface IBroadcast
    {
        void GetAlarmReceiver();
    }
    

    We can trigger it with Xamarin.Forms.DependencyService.Get<IBroadcast>().GetAlarmReceiver();

    Achieve it in Droid folder.

    [assembly: Xamarin.Forms.Dependency(typeof(MyAlarm))]
    namespace ScanDemo.Droid
     {
    
        public class MyAlarm : IBroadcast
      {
        public void GetAlarmReceiver()
        {
            IntentFilter filter = new IntentFilter();
            filter.AddAction(AlarmReceiver.ALARM_TRIGGER);
    
    
            AlarmReceiver receiver = new AlarmReceiver();
            Application.Context.RegisterReceiver(receiver, filter);
         }
       }
    

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited February 21 Accepted Answer

    Here are code for broadcast receiver for system alarm event: `ALARMMANAGER_TRIGGERED

     [BroadcastReceiver]
     [IntentFilter (new[] { AlarmReceiver.ALARM_TRIGGER })]
     public class AlarmReceiver : BroadcastReceiver
     {
      public const string ALARM_TRIGGER = "ALARM_TRIGGERED";
    
      public override void OnReceive(Context context, Intent intent) 
      {
    
        Intent startIntent = new Intent(context, typeof(AlarmIntentService));
        context.StartService(startIntent);
      }
      }
    

    Code for broadcast receiver for system boot-completed event:

       [BroadcastReceiver]
       [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted } )]
       public class BootReceiver : BroadcastReceiver
       {
          public override void OnReceive (Context context, Intent intent)
       {
    
            Intent startIntent = new Intent (context, typeof(BootIntentService));
            context.StartService (startIntent);
       }
       }
    

    Add to AndroidManifest.xml:

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

    If you use Xamarin forms, you can use DependencyService to register BroadcastReceiver.
    Create a interface.

        public  interface IBroadcast
    {
        void GetAlarmReceiver();
    }
    

    We can trigger it with Xamarin.Forms.DependencyService.Get<IBroadcast>().GetAlarmReceiver();

    Achieve it in Droid folder.

    [assembly: Xamarin.Forms.Dependency(typeof(MyAlarm))]
    namespace ScanDemo.Droid
     {
    
        public class MyAlarm : IBroadcast
      {
        public void GetAlarmReceiver()
        {
            IntentFilter filter = new IntentFilter();
            filter.AddAction(AlarmReceiver.ALARM_TRIGGER);
    
    
            AlarmReceiver receiver = new AlarmReceiver();
            Application.Context.RegisterReceiver(receiver, filter);
         }
       }
    
  • MAVZoliMAVZoli Member ✭✭

    @LeonLu said:
    Here are code for broadcast receiver for system alarm event: `ALARMMANAGER_TRIGGERED

     [BroadcastReceiver]
     [IntentFilter (new[] { AlarmReceiver.ALARM_TRIGGER })]
     public class AlarmReceiver : BroadcastReceiver
     {
      public const string ALARM_TRIGGER = "ALARM_TRIGGERED";
    
      public override void OnReceive(Context context, Intent intent) 
      {
      
        Intent startIntent = new Intent(context, typeof(AlarmIntentService));
        context.StartService(startIntent);
      }
      }
    

    Code for broadcast receiver for system boot-completed event:

    [BroadcastReceiver]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted } )]
    public class BootReceiver : BroadcastReceiver
    {
    public override void OnReceive (Context context, Intent intent)
    {

    Intent startIntent = new Intent (context, typeof(BootIntentService));
    context.StartService (startIntent);
    }
    }

    Add to AndroidManifest.xml:

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

    If you use Xamarin forms, you can use DependencyService to register BroadcastReceiver.
    Create a interface.

        public  interface IBroadcast
    {
        void GetAlarmReceiver();
    }
    

    We can trigger it with Xamarin.Forms.DependencyService.Get<IBroadcast>().GetAlarmReceiver();

    Achieve it in Droid folder.

    [assembly: Xamarin.Forms.Dependency(typeof(MyAlarm))]
    namespace ScanDemo.Droid
     {
    
        public class MyAlarm : IBroadcast
      {
        public void GetAlarmReceiver()
        {
            IntentFilter filter = new IntentFilter();
            filter.AddAction(AlarmReceiver.ALARM_TRIGGER);
    
    
            AlarmReceiver receiver = new AlarmReceiver();
            Application.Context.RegisterReceiver(receiver, filter);
         }
       }
    

    Thank you!

Sign In or Register to comment.