【问题标题】:stops getting Geolocation after a while in MAUI (android )在 MAUI (android) 中一段时间​​后停止获取地理定位
【发布时间】:2022-11-21 11:39:49
【问题描述】:

我有一个前台服务,每 5 秒获取一次设备的位置并将其发送到服务器,但一段时间后,前台服务停止获取地理定位数据。


public async Task RunLocation(CancellationToken token)
{
    await Task.Run(async () =>
    {
        if (token.IsCancellationRequested)
            _stopping = true;
        while (!_stopping)
        {
            token.ThrowIfCancellationRequested();
            try
            {
                await Task.Delay(5000, token);

                var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(5));
                var location = await Geolocation.Default.GetLocationAsync(request, token);

                Console.WriteLine("Got location: " + location);
}}}}

【问题讨论】:

  • 为什么你有一个没有catchtry块?这样做的目的是什么?你怎么知道是否有例外?
  • 也许未处理的异常?你怎么知道这个循环还在运行?

标签: maui


【解决方案1】:

如果您的应用程序是针对安卓 10- Q(API 级别 29 或更高)并且正在请求LocationAlways,您还必须添加此权限请求:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

此外,请务必阅读Android documentation on background location updates,因为需要考虑许多限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2019-03-04
    相关资源
    最近更新 更多