简体   繁体   中英

Google Awareness API cannot register location fence (Fence API) Kotlin

Ive been trying to use Google Awareness API (Fence API) and I can register every other kind of fence - headphones, walking- and my broadcast reciever that handles callbacks from fence state changes is working but keep getting com.google.android.gms.common.api.ApiException: 7503 when registering a location fence

Looked for similar posts on stack overflow and made sure I didnt turn off my phones location - below is the code Ive been trying to use for location fence types only

Manifest:

<?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") }
        }
    }

In case anyone gets this problem too.

Have android 10 api 29 on physical device If your testing on a phone/emulator thats on android 29 - you need to add the permission ACCESS_BACKGROUND_LOCATION to the manifest and allow the app to get your location all the time

If your testing on api 30 - you only need "allow while app is in use" to get location fences to register correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM