每次都在xamarin.android

时间:2017-05-05 13:42:18

标签: c# android xamarin.android

我正在尝试在Xamarin.android中创建重复警报,并且正在使用依赖服务从Xamarin.Form触发此警报。

问题是每当我从Xamarin.Forms触发警报时,即使我检查确认是否先前设置了警报,每次都会创建警报。所以在这种情况下,isSchedulerOn()总是返回false。

Referece code for Alarm

这是我的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;

        }

    }

0 个答案:

没有答案