在Android(C#)中启动AlarmManager

编程入门 行业动态 更新时间:2024-10-24 16:33:28
本文介绍了在Android(C#)中启动AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看到并使用了以下链接:创建并在Android中使用AlarmManager调度警报

现在我有了以下代码:

名称空间AlarmManage {公共类MyBroadcastReceiver:BroadcastReceiver {公共重写void OnReceive(上下文上下文,意图) { Toast.MakeText(上下文,时间到了……现在振动了!, ToastLength.Long).Show(); Vibrator振动器=(Vibrator)上下文 .GetSystemService(Context.VibratorService); 振动器。Vibrate(2000); } } } public void startAlertAtParticularTime() { //警报首先在14小时40分钟振动并重复本身以ONE_HOUR间隔 intent = new Intent(this,typeof(MyBroadcastReceiver)); endingIntent = PendingIntent.GetBroadcast( this,280192,intent,PendingIntentFlags.CancelCurrent); Java.Util.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,endingIntent); Toast.MakeText(this,警报将在指定的时间振动,ToastLength.Long).Show(); }

我还在清单中设置了 SET-ALARM和 VIBRATE。 / p>

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

解决方案

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

[BroadcastReceiver(Enabled = true)]

声明此广播,就像在传统android清单中这样声明:

< receiver android: name = MyBroadcastReceiver />

因此, MyBroadcastReceiver 应如下所示:

[BroadcastReceiver(Enabled = true)] 公共类MyBroadcastReceiver:BroadcastReceiver {公共替代无效OnReceive(上下文上下文,意图) { Toast.MakeText(上下文,时间到了……现在振动了!, ToastLength.Long).Show(); Vibrator振动器=(Vibrator)上下文 .GetSystemService(Context.VibratorService); 振动器。Vibrate(2000); } }

现在您可以敬酒:时间到了。 ..现在振动了!!!

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 !!!

解决方案

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 !!!"

更多推荐

在Android(C#)中启动AlarmManager

本文发布于:2023-11-27 04:32:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!