Beispiel #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            workerManager = WorkManager.GetInstance(Application.Context);
            workerManager.CancelAllWork();
            workerManager.PruneWork();
            SetContentView(Resource.Layout.activity_main);

            progressBar = FindViewById <ProgressBar>(Resource.Id.progressBar1);
            textView    = FindViewById <TextView>(Resource.Id.textView1);
            Button createWork = FindViewById <Button>(Resource.Id.button1);

            createWork.Click += (sender, e) =>
            {
                textView.Text = string.Empty;
                var oneTimeWorkRequest = new OneTimeWorkRequest.Builder(typeof(MyWorker))
                                         .AddTag("MyWorker")
                                         .SetInitialDelay(TimeSpan.FromSeconds(10))
                                         .Build();

                workerManager.BeginUniqueWork("MyWorker", ExistingWorkPolicy.Replace, oneTimeWorkRequest).Enqueue();
            };

            Button createListenableWork = FindViewById <Button>(Resource.Id.button2);

            createListenableWork.Click += (sender, e) =>
            {
                textView.Text = string.Empty;
                var periodicWorkRequest =
                    new PeriodicWorkRequest.Builder(typeof(MyListenableWorker), TimeSpan.FromMinutes(10))
                    .SetBackoffCriteria(BackoffPolicy.Linear, TimeSpan.FromSeconds(10))
                    .AddTag("MyListenableWorker")
                    .Build();

                workerManager.Enqueue(periodicWorkRequest);
                workerManager.GetWorkInfosByTagLiveData("MyListenableWorker").Observe(this, this);
            };

            Button cancelListenableWork = FindViewById <Button>(Resource.Id.button3);

            cancelListenableWork.Click += (sender, e) =>
            {
                workerManager.CancelAllWorkByTag("MyListenableWorker");
            };
        }