如何解决Google Awareness API无法注册位置围栏Fence APIKotlin
我一直在尝试使用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>
MainActivity.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 如果您在Android 29的手机/模拟器上进行测试-您需要添加权限 ACCESS_BACKGROUND_LOCATION到清单,并允许该应用始终获取您的位置
如果您在api 30上进行测试-您只需要“在使用应用程序时允许”即可获取位置信息栏以正确注册
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。