簡體   English   中英

當應用程序在后台 xamarin forms android 應用程序在升級目標 Z8A5DA52ED126447D359E70C05721AA8 時,后台位置不起作用

[英]background location not working when app in background xamarin forms android app when upgrade target api to 29

i have xamarin forms app that have tracking and fore ground service for background tracking working fine when set android target api level 28 but after upgrade it target api level 29 to upload app to play store no background location permission set to app so cannot get location when應用程序在后台。

這個清單權限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

以及主要活動中的此運行時請求:

     private static string[] _initialPerms ={
        Manifest.Permission.AccessFineLocation,
        Manifest.Permission.AccessCoarseLocation
    };
    
 if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == Permission.Denied || ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == Permission.Denied)
        {
            RequestPermissions(_initialPerms, 1337);
        }

謝謝。

從 Android 10 開始,后台位置作為獨立資源出現。 除了前台權限之外,應用程序還必須明確請求此權限。

@TargetApi(29)
private fun Context.checkLocationPermissionAPI29(locationRequestCode : Int) {
    if (checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION) &&
        checkSinglePermission(Manifest.permission.ACCESS_COARSE_LOCATION) &&
        checkSinglePermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) return
    val permList = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, 
                           Manifest.permission.ACCESS_COARSE_LOCATION,
                           Manifest.permission.ACCESS_BACKGROUND_LOCATION)
    requestPermissions(permList, locationRequestCode)
    
}

private fun Context.checkSinglePermission(permission: String) : Boolean {
    return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}

ACCESS_BACKGROUND_LOCATION權限是Android 10.0之后的新權限。 Even if you have set the target version to Api 29, but the version of support SDK in Xamarin.Android is still v28.xxx (Android 9.0).So this enumeration is still unavailable in Xamarin.Android now. 您需要的只是等待支持 SDK 的更新。

在此處輸入圖像描述

在您的情況下, ACCESS_BACKGROUND_LOCATION將與舊版本兼容。 如果應用程序申請ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION ,系統會在構建過程中自動添加權限ACCESS_BACKGROUND_LOCATION

============================更新====================== =========

在 Android 10(API 級別 29)及更高版本上,您必須在應用的清單中聲明ACCESS_BACKGROUND_LOCATION權限,以便在運行時請求后台位置訪問 在早期版本的 Android 上,當您的應用程序接收前台位置訪問權限時,它也會自動接收后台位置訪問權限。

<manifest ... >
  <!-- Required only when requesting background location access on
       Android 10 (API level 29) and higher. -->
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>

更多信息請參閱此處的 android 文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM