Starting AlarmManager in Android (C#)

◇◆丶佛笑我妖孽 提交于 2020-01-05 03:50:30

问题


I saw and used this link: Creating And Scheduling Alarms Using AlarmManager In Android

Now I have this code:

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

I also set "SET-ALARM" and "VIBRATE" in Manifest.

My problem: I do not see the output "Time Up... Now Vibrating !!!


回答1:


I think the problem is that you didn't successfully declare your MyBroadcastReceiver, in xamarin, we can use:

[BroadcastReceiver(Enabled = true)] 

to declare this broadcast. It's like declare it in traditional android manifest like this:

<receiver android:name="MyBroadcastReceiver" /> 

So, MyBroadcastReceiver should be like this:

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

Now you can get the toast: "Time Up... Now Vibrating !!!"



来源:https://stackoverflow.com/questions/43468602/starting-alarmmanager-in-android-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!