Xamarin Android - How to schedule and alarm with a

2019-06-13 19:46发布

问题:

(I'm asking here because I didn't get help at Xamarin forums) 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);
    }

Before that code is called, my class makes sure that these have also been executed:

ComponentName receiver = new ComponentName(context, Java.Lang.Class.FromType(typeof(AlarmReceiver)));
        PackageManager pm = context.PackageManager;
        pm.SetComponentEnabledSetting(receiver, ComponentEnabledState.Enabled, ComponentEnableOption.DontKillApp);

        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);

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();
        }
    }

Okay, so the receiver is being registered by Xamarin correctly. I know this because if I give an incorrect tick value to AlarmClockInfo (a value outside of DateTime's tick range) the alarm goes off immediately and my OnReceive method is called. However when I give it a tick value, say a minute ahead of the current time, the alarm doesn't go off. Maybe the time is wrong?... Doesn't seem so because I have logged the time of the the system's next scheduled alarm and it reports back with the same time I set it for. Any thoughts?

EDIT: So I already have an android app that performs all this correctly. When I convert it to Xamarin and C#, it no longer works.

回答1:

This is how I'm creating a local notification in my Xamarin app.

DateTime time = ... // whatever time
AlarmManager manager = (AlarmManager)context.GetSystemService(Context.AlarmService);

Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();

calendar.Set(time.Year, time.Month - 1, time.Day, time.Hour, time.Minute, 0);
manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis,
AlarmManager.IntervalDay, pendingIntent);

And here is the BroadcastReceiver class:

[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{

    public override void OnReceive(Context context, Intent intent)
    {
        NotificationManager nManager = (NotificationManager)context.GetSystemService(Context.NotificationService);

        Intent repeatingIntent;

        // Here I'm opening two different Activities based on condition
        if (CommonUtils.isLoggedIn()))
        {
            repeatingIntent = new Intent(context, typeof(MainActivity));
            repeatingIntent.PutExtra(MainActivity.SELECT_TAB, 1);
        }
        else
        {
            repeatingIntent = new Intent(context, typeof(SplashActivity));
        }

        repeatingIntent.SetFlags(ActivityFlags.ClearTop);

        PendingIntent pIntent = PendingIntent.GetActivity(context, 100, repeatingIntent, PendingIntentFlags.UpdateCurrent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .SetContentIntent(pIntent)
            .SetSmallIcon(Resource.Drawable.az_logo_small)
            .SetColor(ContextCompat.GetColor(context, Resource.Color.PrimaryColor))
            .SetContentTitle(CommonUtils.MAIN_TITLE)
            .SetContentText(UIMessages.VITAL_REMINDER)
            .SetAutoCancel(true);

        nManager.Notify(100, builder.Build());
    }
}

and in AndroidManifest.xml, you need this permission

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

also need the register the BroadcastReceiver in AndroidManifest.xml

<application android:label="YourAppName" android:largeHeap="true" android:icon="@drawable/ic_launcher">
    <receiver android:name=".AlarmReceiver"></receiver>
</application>

Hope it helps.



回答2:

First you have to Declare <receiver android:name=".AlarmReceiver"></receiver> in your Manifest inside yout app

And create a BroadcastReceiver class:

[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        //Miramos si la alarma debe repetirse
        var repeat = intent.GetStringExtra("repeat");

        //Hacemos sonar la alarma
        Uri notification = RingtoneManager.GetDefaultUri(RingtoneType.Alarm);
        Ringtone r = RingtoneManager.GetRingtone(context, notification);
        r.Play();

        //
    }

The following code is to Start and cancel the pending intent

public void SetAlarm(long miliseconds)
        {
            AlarmManager alarmManager = (AlarmManager)this.Activity.GetSystemService(Context.AlarmService);
            Intent intent = new Intent(this.Activity, typeof(AlarmReceiver));

            intent.PutExtra("repeat", repeat);

            PendingIntent pendingIntent = PendingIntent.GetBroadcast(this.Activity, /*id de la alarma que sea unico */0, intent, PendingIntentFlags.CancelCurrent);

            alarmManager.Set(AlarmType.RtcWakeup, miliseconds, pendingIntent);

            Toast toast = Toast.MakeText(this.Activity, Resource.String.set_alarm, ToastLength.Short);

            toast.Show();
        }

        public void CancelAlarm()
        {
            AlarmManager alarmManager = (AlarmManager)this.Activity.GetSystemService(Context.AlarmService);
            Intent intent = new Intent(this.Activity, typeof(AlarmReceiver));

            PendingIntent pendingIntent = PendingIntent.GetBroadcast(this.Activity, /*a traves del anterior id ahora podemos pedir que se actualice */0, intent, PendingIntentFlags.UpdateCurrent);

            //Con el pending intent actualizado podemos cancelarlo
            pendingIntent.Cancel();
            alarmManager.Cancel(pendingIntent);

            Toast toast = Toast.MakeText(this.Activity, Resource.String.remove_alarm, ToastLength.Short);

            toast.Show();
        }

To finish you have to call them for example when clicking them

        if (alarm.Active == true)
        {
            alarm.Active = false;
            alarmsFragment.CancelAlarm();
        }
        else
        {
            alarm.Active = true;
// the time you want in milliseconds
        long miliseconds = Java.Lang.JavaSystem.CurrentTimeMillis() + 10000  ;
        alarmsFragment.SetAlarm(miliseconds);
    }