应用程序在Windows 10中处于后台时的位置跟踪?

时间:2016-03-30 11:49:42

标签: c# win-universal-app windows-10

我是Windows 10新手,目前正致力于基于位置的应用。我的要求是跟踪特定时间间隔的用户位置,并每10分钟将数据发送到服务器。有人可以建议在Windows 10中是否可以这样做吗?我不知道这一点。

更新

我也想在应用程序处于后台时执行上述操作。我尝试了下面的代码

 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);
        }
    }

在这些中,我只在应用程序处于前台或最小化时获取位置详细信息。如果我杀了应用程序,它不会给我位置详细信息。另外,请帮助我如何在后台任务中使用它以及如何触发时间触发器将数据发送到服务器,即使用户杀死了应用程序?

另外,我们可以使用多个后台任务吗?我想使用一个用于TimeTrigger将数据发送到服务器,另一个用于推送通知目的。

3 个答案:

答案 0 :(得分:3)

完全可以访问Windows 10中的用户位置。 它可以从不同的来源访问,如;

  • GPS:大约10米内
  • Wi-Fi:约30米至500米
  • 电池塔:约300米至3,000米
  • IP地址:介于约1,000米至5,000米之间

有关技术理解,请参阅以下链接。

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

对于来自频道的视频,请参阅

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

这些是可以参考的好文章。

更多技术细节:

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

和一些例子,

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

答案 1 :(得分:1)

完全有可能。但是,如果用户拒绝提供位置,那么你就可以了。

<强>更新

要在后台跟踪,您需要使用ExtendedExecutionSession代码示例:http://www.sharpgis.net/post/2015/03/29/Using-Windows-10s-Extended-Execution

答案 2 :(得分:1)

  

在这些中,我只在应用程序处于前台或最小化时获取位置详细信息。如果我杀了应用程序,它不会给我位置详细信息。

扩展执行要求用户打开您的应用程序并将其放在他们知道正在运行的后台堆栈中。以下是有关扩展执行的教程:https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution

  

另外,请帮助我如何在后台任务中使用它以及如何触发时间触发器将数据发送到服务器,即使用户杀死了应用程序?

扩展执行位置跟踪不能在后台任务中使用。但是,您可以使用LocationTrigger并在每次用户移出地理围栏区域时记录新位置。截至周年更新位置触发器和时间触发器可以注册为进程内或进程外任务:https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-an-inproc-background-task

以下是进程内后台任务的示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundActivation

  

另外,我们可以使用多个后台任务吗?我想使用一个用于TimeTrigger将数据发送到服务器,另一个用于推送通知目的。

是的,您可以使用BackgroundTaskBuilder注册任意多种不同类型的任务,它们可以在您的应用程序进程内运行,也可以作为单独的后台任务进程运行。

以下是使用BackgroundTaskBuilder的教程:https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task

以下是进程外后台任务的示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTask