gpt4 book ai didi

c# - 警报未访问 BroadcastReceiver

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

我有一个时间选择器来设置闹钟

public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
{
public static readonly string TAG = "MyTimePickerFragment";
Action<DateTime> timeSelectedHandler = delegate { };

public static TimePickerFragment NewInstance(Action<DateTime> onTimeSelected)
{
TimePickerFragment frag = new TimePickerFragment();
frag.timeSelectedHandler = onTimeSelected;
return frag;
}

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
DateTime currentTime = DateTime.Now;
//bool is24HourFormat = DateFormat.Is24HourFormat(Activity);
TimePickerDialog dialog = new TimePickerDialog
(Activity, this, currentTime.Hour, currentTime.Minute, false);
return dialog;
}



public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
{
DateTime currentTime = DateTime.Now;
DateTime selectedTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, hourOfDay, minute, 0);
Log.Debug(TAG, selectedTime.Ticks.ToString());

timeSelectedHandler(selectedTime);
}
}

这个函数用来设置闹钟

 private void setAlarm(DateTime time)
{
Intent intent = new Intent(this, typeof(MyReceiver));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
alarmManager.Set(AlarmType.RtcWakeup, time.Ticks, pendingIntent);
string timeSet = time.Ticks.ToString();
Toast.MakeText(this, timeSet, ToastLength.Short).Show();

}

即使设置的闹钟等于当前时间,也不会调用 broadcastreceiver。我试过检查time time set和current time的值,即使当前时间到了set time,broadcastreceiver也不生效

广播接收者

[BroadcastReceiver]
public class MyReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Time reached!", ToastLength.Short).Show();
}
}
}

我是缺少权限还是其他原因?

更新:感谢 sushihangover 的回答

public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
{
public static readonly string TAG = "MyTimePickerFragment";
Action<Calendar> timeSelectedHandler = delegate { };

public static TimePickerFragment NewInstance(Action<Calendar> onTimeSelected)
{
TimePickerFragment frag = new TimePickerFragment();
frag.timeSelectedHandler = onTimeSelected;
return frag;
}

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
DateTime currentTime = DateTime.Now;
//bool is24HourFormat = DateFormat.Is24HourFormat(Activity);
TimePickerDialog dialog = new TimePickerDialog
(Activity, this, currentTime.Hour, currentTime.Minute, false);
return dialog;
}

public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
{
Calendar calNow = Calendar.Instance;
Calendar calSet = (Calendar)calNow.Clone();
calSet.Set(CalendarField.HourOfDay, hourOfDay);
calSet.Set(CalendarField.Minute, minute);

calSet.Set(CalendarField.Millisecond, 0);

DateTime currentTime = DateTime.Now;
DateTime selectedTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, hourOfDay, minute, 0);
Log.Debug(TAG, selectedTime.Ticks.ToString());

timeSelectedHandler(calSet);
}
}

最佳答案

您正在使用 .Net 的 DateTime 中的刻度,而 Android 的 AlarmManager 基于 Android/Linux Epoch 毫秒:

查看我对这些其他 SO 的回答,了解如何使用 Android 系统时间或 Android 日历:

关于c# - 警报未访问 BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53975027/

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