定位服务不再后台运行

时间:2019-05-26 01:06:07

标签: xamarin.forms xamarin.android

我在应用中使用Xamarin's Background Location demo中的代码已有一年多了,直到大约两周前,背景位置更新才出现任何问题。从下面的代码中,当应用程序处于后台时,我不再看到日志:

LocationService.cs

#region ILocationListener implementation
        // ILocationListener is a way for the Service to subscribe for updates
        // from the System location Service

        public void OnLocationChanged(Android.Locations.Location location)
        {
            this.LocationChanged(this, new LocationChangedEventArgs(location));

            Log.Debug(logTag, DateTime.UtcNow.ToString());

            Log.Debug(logTag, "---- LOCATION UPDATED");

            if (!Settings.IsLoggedIn)
            {
                return;
            }
            else if (Settings.IsAuthExpired)
            {
                Task.Run(() => loginStore.Refresh());
            }

            // This should be updating every time we request new location updates
            // both when the app is in the background, and in the foreground
            //Log.Debug(logTag, String.Format("Latitude is {0}", location.Latitude));
            //Log.Debug(logTag, String.Format("Longitude is {0}", location.Longitude));

            Settings.Latitude = location.Latitude;
            Settings.Longitude = location.Longitude;

            Task.Run(() => dataStore.UpdateLocation(location.Latitude, location.Longitude));
        }

这是我的清单文件(没有我的应用程序的名称,等等)。我检查了一下历史记录,发现该文件仅进行了8个月的版本号更改(因此过去几周内没有什么大问题可以保证背景位置出现问题)。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="60" android:versionName="0.1.7" package="REDACTED">
    <uses-sdk android:minSdkVersion="26" android:targetSdkVersion="28" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" />
    <application android:label="REDACTED" android:icon="@drawable/logo_size_icon">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="REDACTED.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
        </provider>
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

在Xamarin Repo上的以下已解决问题中找到了答案:https://github.com/xamarin/mobile-samples/issues/99