在Google的Geofencing教程中没有看到Geofence Transitions

use*_*716 7 android location geofencing android-geofence

我正在尝试使用Google的示例测试地理围栏功能:创建和监控地理围栏.我在这里上传了代码.

问题是我从未收到进入或退出的通知.我有Wifi,4G和GPS.我试过用以下方法测试:

  1. 我甚至走出我的房子大约50英尺,然后走回来 - 但没有通知.我可以看到播放服务已连接,甚至"MainActivity.java中的GeofenceUtils.ACTION_GEOFENCES_ADDED"被触发,所以我认为Geofences正在被正确添加.
  2. 在设置中启用模拟位置并使用此假GPS应用程序并更改位置 - 从与Geofence1相同的坐标开始,然后设置为完全外部(在另一个状态) - 但我仍然没有得到退出通知.

我究竟做错了什么?ANyone成功运行了这个谷歌的例子:创建和监控地理围栏.我在这里上传了代码以便于浏览.

我只是想学习Geofencing - 检测进入和退出.我将标记任何答案作为我可以用来在我的真实设备上进行地理工作的正确答案

我输入下面的坐标和半径.

小智 17

我也遇到了示例代码的问题.我遇到的一个主要问题是声明ReceiveTransitionsIntentService.您不需要添加BroadcastReceiver或请求位置更新,但您需要更改AndroidManifest以将导出值设置为true,如下所示:

<service
    android:name=".ReceiveTransitionsIntentService"
    android:exported="true" >
</service>
Run Code Online (Sandbox Code Playgroud)

确保服务的路径也正确.

我已经大大简化了Google示例,您可以在此处找到代码和说明.我删除了UI组件和持久的Geofence存储,因此这个示例应该更容易理解.希望这可以帮助!


小智 0

最初我的代码运行顺利,但之后我也遇到了这个问题,请通过修改 continueAddGeofences() 函数中的 geofenceRequester 类来尝试此操作。基本上是通过位置请求方法调用接收者

 private void continueAddGeofences() {


            // Get a PendingIntent that Location Services issues when a geofence
            // transition occurs
            mGeofencePendingIntent = createRequestPendingIntent();

            // Send a request to add the current geofences
            mLocationClient.addGeofences(mCurrentGeofences, mGeofencePendingIntent,
                    this);
            //mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            try {
                LocationRequest locationrequest = LocationRequest.create();
                locationrequest.setPriority(locationrequest.PRIORITY_HIGH_ACCURACY);
                 locationrequest.setInterval(3000);


                mLocationClient.requestLocationUpdates(locationrequest, mGeofencePendingIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
Run Code Online (Sandbox Code Playgroud)