Geofence does not start in the background on Windows Phone 8.1.

I am trying to implement geoprocessing on a Windows 8.1 phone. At first I wanted to create a sample project to understand how it works, but I could not get it to work. What I'm trying to achieve, basically, I will set the coordinates and close the application by clicking the "Back" button, and this will cause a notification about the toast when the phone is in the area of ​​interest.

I created an empty Windows phone (silverlight) 8.1 Project (geofence_test_01) and added the Windows RT component component (BackgroundTask) to the same solution. Added link for BackgroundTask in project geofence_test_01.

Solution explorer

The application manifest includes ID_CAP_LOCATION.

WMAppManifest.xml

MainPage.xaml has only one button to start geoprocessing.

<Button Name="btnStart" Content="Start" Click="btnStart_Click"/>

btnSave_Click , .

private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        Init_BackgroundGeofence();
        registerBackgroundTask();  
    }

private async Task Init_BackgroundGeofence()
        {
            //----------------- Crating Geofence ---------------
            var geofenceMonitor = GeofenceMonitor.Current;
            var geoId = "building9";
            var positionBuilding9 = new BasicGeoposition()
            {
                Latitude = 47.6397,
                Longitude = -122.1289
            };
            var geofence = new Geofence(geoId, new Geocircle(positionBuilding9, 100),
                MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited,
                false, TimeSpan.FromSeconds(10));
            geofenceMonitor.Geofences.Add(geofence);
        }

private async Task registerBackgroundTask()
    {
        //----------------- Register Background Task ---------------
        var backgroundAccessStatus =
            await BackgroundExecutionManager.RequestAccessAsync();
        var geofenceTaskBuilder = new BackgroundTaskBuilder
        {
            Name = "GeofenceBackgroundTask",
            TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask"
        };

        var trigger = new LocationTrigger(LocationTriggerType.Geofence);
        geofenceTaskBuilder.SetTrigger(trigger);
        var geofenceTask = geofenceTaskBuilder.Register();
    }

, , BackgroundTask :

namespace BackgroundTask
{
    public sealed class GeofenceBackGroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            var geofenceMonitor = GeofenceMonitor.Current;
            var geoReports = geofenceMonitor.ReadReports();
            var geoId = "building9";
            foreach (var geofenceStateChangeReport in geoReports)
            {
                var id = geofenceStateChangeReport.Geofence.Id;
                var newState = geofenceStateChangeReport.NewState;
                if (id == geoId && newState == GeofenceState.Entered)
                {
                  //------ Call NotifyUser method when Entered -------
                    notifyUser(); 
                }
            }
        }

        private void notifyUser()
        {
            var toastTemplate = ToastTemplateType.ToastText02;
            var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate);
            var textElements = toastXML.GetElementsByTagName("text");
            textElements[0].AppendChild(toastXML.CreateTextNode("You are in!"));

            var toast = new ToastNotification(toastXML);

            ToastNotificationManager.CreateToastNotifier().Show(toast);
        }
    }
}

. backgroundTask, , . . "", - , , . , - , ...

:

http://www.jayway.com/2014/04/22/windows-phone-8-1-for-developers-geolocation-and-geofencing/

Geofence Windows Phone 8.1 (WinRT)

Toast Geofence Windows Phone 8.1

http://java.dzone.com/articles/geofencing-windows-phone-81

: https://drive.google.com/file/d/0B8Q_biJCWl4-QndYczR0cjNhNlE/view?usp=sharing

----

Romasz, Lifecycle, " " registerBackgroundTask(). , registerBackgroundTask() - /.

+4
2

( ), , , , . GitHub.

:

  • WMAppManifest (IS_CAP_LOCATION) Package.appxmanifest(Location)
  • ( , ..) BackgroundTask
  • , BackgroundTask Windows Runtime

, , , , , .

0

​​ Package.appxmanifest Declarations ( , )? enter image description here

0

Source: https://habr.com/ru/post/1569402/


All Articles