Google Awareness API无法注册位置围栏(围栏API)Kotlin



我一直在尝试使用Google Awareness API(Fence API(,我可以注册所有其他类型的围栏-耳机、步行-我的广播接收器可以处理围栏状态更改的回叫,但在注册位置围栏时,会继续获得com.Google.android.gms.common.API.ApiException:7503

在堆栈溢出上查找类似的帖子,并确保我没有关闭手机定位-下面是我试图用于仅类型的定位围栏的代码

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.location_try_dos">
<!-- Needed For Awarness - Fence API -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".FenceReciever"
android:enabled="true"
android:exported="false"></receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.awareness.API_KEY"
android:value="MYKEY" />
</application>
</manifest>

主要活动.kt

override fun onStart() {
super.onStart()
Timber.i("onStart Called")
//there was two optations for manifest
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,
"Have Permissions Needed",
Toast.LENGTH_LONG).show()
val locationFence:AwarenessFence = LocationFence.entering(37.4220,-122.0841, 1609.0)

Awareness.getFenceClient(this).updateFences(FenceUpdateRequest.Builder()
.addFence(FENCE_KEY, locationFence,
PendingIntent.getBroadcast(
this,
FENCE_INTENT_ID,
Intent(this,FenceReciever::class.java),
PendingIntent.FLAG_UPDATE_CURRENT))
.build())
.addOnSuccessListener { Log.i("FENCE API", "Fence was successfully registered.") }
.addOnFailureListener { e -> Log.e("FENCE API", "Fence could not be registered: $e") }
}
}

如果有人也遇到这个问题。

在物理设备上拥有android 10 api 29如果你在安卓29上的手机/模拟器上进行测试,你需要添加权限ACCESS_BACKGROUND_LOCATION到清单,并允许应用程序始终获取您的位置

如果你在api 30上测试,你只需要";当应用程序正在使用时允许";使位置围栏正确注册

最新更新