Location tracking when app is in background in Win

2019-07-25 15:42发布

问题:

I am new to Windows 10 and currently working on Location based apps. My requirement is to track user locations for a particular time interval and send data to the server every 10 minutes. Can someone suggest whether this is possible in Windows 10 or not? I am not aware of this.

Update

I also want to do the above in when the app is in the background also.I tried below code

 protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.New)
        {
            var extendedSession = new ExtendedExecutionSession();
            extendedSession.Reason = ExtendedExecutionReason.LocationTracking;
            extendedSession.Description = "Location tracking";

            ExtendedExecutionResult result = await extendedSession.RequestExtensionAsync();
            if (result == ExtendedExecutionResult.Allowed)
            {
                Debug.WriteLine("Background execution approved");
            }
            else
            {
                Debug.WriteLine("Background execution denied");
            }

            Geolocator locator = new Geolocator();
            locator.DesiredAccuracyInMeters = 0;
            locator.MovementThreshold = 500;
            locator.DesiredAccuracy = PositionAccuracy.High;
            locator.PositionChanged += Locator_PositionChanged;
        }
    }

  private void Locator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    {
        Debug.WriteLine(args.Position.Coordinate.Latitude.ToString("0.00000000") + " " + args.Position.Coordinate.Longitude.ToString("0.00000000"));
        if (MCSManager.Instance.userDetails != null && MCSManager.Instance.userDetails.LOC_TRACK_ENABLED.Equals("1") && userSettings.Values.ContainsKey(Constants.USER_ID))
        {
            DatabaseManager dbManager = new DatabaseManager();
            Location_Tracking location_tracking = WebserviceED.StoreLocationData(args.Position.Coordinate.Latitude.ToString(),
                args.Position.Coordinate.Longitude.ToString(), WebserviceED.getTimestamp(), args.Position.Coordinate.Accuracy.ToString());
            var insertSuccessfull = dbManager.insertSingleRecord(location_tracking);
        }
    }

in these, I only get location details when the app is in the foreground or minimized. If I kill the app, it doesn't give me location details. Also, please help me how to used this in Background Tasks and how to fire a time trigger to send data to a server even if user kills the app?

Also, can we use more than one Background Tasks? I want to use one for TimeTrigger to send data to a server and another one for Push Notification purpose.

回答1:

It totally possible to access to Location of user in windows 10. It can be access from different source like;

  • GPS : within approximately 10 meters
  • Wi-Fi : between approximately 30 meters and 500 meters
  • Cell towers : between approximately 300 meters and 3,000 meters
  • IP address : between approximately 1,000 meters and 5,000 meters

For Technical understanding you can refer following link.

https://msdn.microsoft.com/library/windows/apps/windows.devices.geolocation.aspx

and for the video from channel , refer

https://channel9.msdn.com/Series/Windows-10-development-for-absolute-beginners/UWP-059-UWP-Weather-Accessing-the-GPS-Location

These are good articles to refer.

For Some more technical Detail:

https://msdn.microsoft.com/en-us/library/windows/apps/mt219698.aspx

and kind of example,

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MapControl



回答2:

Its totally possible. But if user refuse to give location, then you can't.

UPDATE

For tracking in background, you need to use ExtendedExecutionSession, code sample here: http://www.sharpgis.net/post/2015/03/29/Using-Windows-10s-Extended-Execution



回答3:

in these, I only get location details when the app is in the foreground or minimized. If I kill the app, it doesn't give me location details.

Extended Execution requires the user to have opened your app and have it in the backstack where they are aware of it running. Here is the tutorial on Extended Execution: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution

Also, please help me how to used this in Background Tasks and how to fire a time trigger to send data to a server even if user kills the app?

Extended Execution Location Tracking cannot be used in a background task. However, you could use a LocationTrigger and record the new location every time the user moves out of a geofenced area. As of the Anniversary Update Location Trigger and Time Trigger can be registered as in-proc or out-of-proc tasks: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-an-inproc-background-task

Here is the sample for in-proc background tasks: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundActivation

Also, can we use more than one Background Tasks? I want to use one for TimeTrigger to send data to a server and another one for Push Notification purpose.

Yes, you can register as many separate kinds of tasks as you need with BackgroundTaskBuilder and they can run inside your application process or as a separate background task process.

Here is the tutorial on using BackgroundTaskBuilder: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task

Here is the sample for out-of-proc background tasks: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTask