Search code examples
c#androidxamarinandroid-intentalarmmanager

Setting Intent for AlarmManager


To be able to create an alarm manager implementation, you have to use an intent and a pending intent, according to all the references I saw, however I get these errors, and I'm not sure why because my code is almost identical to others.

There are 2 errors. This is the code with the 2 errors for both Intent and PendingIntent:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.Content;
using Android.App;

namespace Assignment {
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChooseAlarm : ContentPage {
    public ChooseAlarm() {
        InitializeComponent();
        var donebutton = this.FindByName<Button>("doneButton");

        donebutton.Clicked += donebutton_Clicked;
    }

    private void donebutton_Clicked(object sender, EventArgs e) {
        StartAlarm();
    }
    private void StartAlarm() {
        AlarmManager manager = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);

        Intent myIntent = new Intent(this, typeof(AlarmNotificationReceiver)); --Argument 2: cannot convert from 'System.Type' to 'Java.Lang.Class'

        PendingIntent  pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0); --Argument 1: cannot convert from 'Assignment.ChooseAlarm' to 'Android.Content.Context', same for above

        manager.Set(AlarmType.RtcWakeup, Android.OS.SystemClock.ElapsedRealtime() + 4000, pendingIntent);
    }
}
}

Receiver code, no errors:

using Assignment.Droid;
using Android.OS;
using Xamarin.Android;
using Android.Content;
using Android.Support.V4.App;
using Android.App;

namespace Assignment {
[BroadcastReceiver(Enabled = true)]
public class AlarmNotificationReceiver : BroadcastReceiver {
    public override void OnReceive(Context context, Intent intent) {
        NotificationCompat.Builder builder = new 
NotificationCompat.Builder(context);

        builder.SetAutoCancel(true)
            .SetDefaults((int)NotificationDefaults.All)
            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle("Alarm Actived!")
            .SetContentText("test")
            .SetContentInfo("Info");

        NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);
        manager.Notify(1, builder.Build());
    }
}
}

Solution

  • Here you should have activity, Which inherits Activity or AppCompatActivity

    Intent myIntent = new Intent(this, typeof(MainActivity)); 
    

    & here your current class should be activity. Than you keyword this going to work.

    PendingIntent  pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);
    

    What I realize that you are using Android code in Xamarin forms shared project which is not going to work. You need to have Xamarin.forms code there. Otherwise use it in Android project.