我正在尝试在Xamarin.android
中创建重复警报,并且正在使用依赖服务从Xamarin.Form
触发此警报。
问题是每当我从Xamarin.Forms
触发警报时,即使我检查确认是否先前设置了警报,每次都会创建警报。所以在这种情况下,isSchedulerOn()
总是返回false。
这是我的Xamarin.Form
代码,我从App.cs
DependencyService.Get<ISyncService>().startScheduler();
这是我的Xamarin.Android
public class SyncScheduler : ISyncService
{
private Intent syncServiceIntent;
private AlarmManager syncAlarmManager;
private PendingIntent syncPendingIntent;
private int REQUEST_SYNC_SCHEDULE = 200;
private long SYNC_SCHEDULER_INTERVAL = 30 * 1000;
private readonly String TAG ="SyncScheduler";
public void startScheduler()
{
if (!isSchedulerOn())
{
syncServiceIntent = new Intent(Android.App.Application.Context, typeof(SyncService));
syncAlarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
syncPendingIntent = PendingIntent.GetService(Android.App.Application.Context,
REQUEST_SYNC_SCHEDULE,
syncServiceIntent,
PendingIntentFlags.CancelCurrent);
syncAlarmManager.SetRepeating(AlarmType.Rtc, DateTime.Now.Millisecond, SYNC_SCHEDULER_INTERVAL, syncPendingIntent);
Log.Error(TAG, "Sync: Scheduler created " );
}
else
{
Log.Error(TAG, "Sync: Scheduler Already Running " );
}
}
public bool isSchedulerOn()
{
if (syncServiceIntent != null)
return PendingIntent.GetBroadcast(Android.App.Application.Context, REQUEST_SYNC_SCHEDULE, syncServiceIntent, PendingIntentFlags.NoCreate) != null;
else return false;
}
}