无法在 Xamarin Android C# 中创建警报
Cant create an alarm in Xamerin Android C#
复制了我老师的简化代码,以查看我编写的代码是否有误或其他问题,但即使是她的代码也无法正常工作。在这里:
private void BtnAlarm_Click(object sender, EventArgs e)
{
Intent intentAlarm = new Intent(this, typeof(AlarmReceiver));
// #2 - Pending intent - > Broadcast
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Application.Context, 1, intentAlarm, 0);
// #3 - Set Alarm Manager
AlarmManager alarmManager = (AlarmManager)GetSystemService(AlarmService);
//30s
alarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup,
SystemClock.ElapsedRealtime() + 30 * 1000, pendingIntent);
}
调试器不会 运行 到此 activity:
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}
需要说明的是,运行在她的应用程序中,闹钟正在工作。
您可以将下面的代码与您的 AlarmReceiver
:
[BroadcastReceiver(Enabled = true, Exported = false)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}
复制了我老师的简化代码,以查看我编写的代码是否有误或其他问题,但即使是她的代码也无法正常工作。在这里:
private void BtnAlarm_Click(object sender, EventArgs e)
{
Intent intentAlarm = new Intent(this, typeof(AlarmReceiver));
// #2 - Pending intent - > Broadcast
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Application.Context, 1, intentAlarm, 0);
// #3 - Set Alarm Manager
AlarmManager alarmManager = (AlarmManager)GetSystemService(AlarmService);
//30s
alarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup,
SystemClock.ElapsedRealtime() + 30 * 1000, pendingIntent);
}
调试器不会 运行 到此 activity:
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}
需要说明的是,运行在她的应用程序中,闹钟正在工作。
您可以将下面的代码与您的 AlarmReceiver
:
[BroadcastReceiver(Enabled = true, Exported = false)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}