gpt4 book ai didi

android - 在 Android 中启动 AlarmManager (C#)

转载 作者:行者123 更新时间:2023-11-30 00:35:34 25 4
gpt4 key购买 nike

我看到并使用了这个链接: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();
}

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

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

最佳答案

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

[BroadcastReceiver(Enabled = true)] 

声明此广播。这就像在传统的 android list 中这样声明它:

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

现在您可以 toast 了:“时间到...现在振动!!!”

关于android - 在 Android 中启动 AlarmManager (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com