我看到并使用了以下链接:Creating And Scheduling Alarms Using AlarmManager In Android

现在我有这段代码:

namespace AlarmManage
{
    public class MyBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Time Up... Now Vibrating !!!",
                    ToastLength.Long).Show();
            Vibrator vibrator = (Vibrator)context
                    .GetSystemService(Context.VibratorService);
            vibrator.Vibrate(2000);
        }
    }
}

public void startAlertAtParticularTime()
{
    // alarm first vibrate at 14 hrs and 40 min and repeat itself at ONE_HOUR interval
    intent = new Intent(this, typeof(MyBroadcastReceiver));
    pendingIntent = PendingIntent.GetBroadcast(
            this, 280192, intent, PendingIntentFlags.CancelCurrent);
    Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
    calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
    calendar.Set(Java.Util.CalendarField.HourOfDay, 14);
    calendar.Set(Java.Util.CalendarField.Minute, 49);
    alarmManager = (AlarmManager)GetSystemService(AlarmService);
    alarmManager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis,
            AlarmManager.IntervalHour, pendingIntent);
    Toast.MakeText(this, "Alarm will vibrate at time specified", ToastLength.Long).Show();
}


我还在清单中设置了“ SET-ALARM”和“ VIBRATE”。

我的问题:我看不到输出“ Time Up ... Now Vibrating !!!”

最佳答案

我认为问题在于您没有在xamarin中成功声明您的MyBroadcastReceiver,我们可以使用:

[BroadcastReceiver(Enabled = true)]


宣布此广播。就像在传统的android清单中声明它一样:

<receiver android:name="MyBroadcastReceiver" />


因此,MyBroadcastReceiver应该是这样的:

[BroadcastReceiver(Enabled = true)]
public class MyBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, "Time Up... Now Vibrating !!!",
                ToastLength.Long).Show();
        Vibrator vibrator = (Vibrator)context
                .GetSystemService(Context.VibratorService);
        vibrator.Vibrate(2000);
    }
}


现在您可以祝酒:“ Time Up ... Now Vibrating !!!”

08-26 10:10