我在哪里可以找到Android Studio 3.3的非弃用地理围栏示例代码?

问题描述 投票:-1回答:1

奇怪的问题,但我找不到在Android上进行地理围栏的工作代码示例。看起来有一些官方地理围栏样本:

  1. Deprecated Android Geofencing app
  2. Sample code for Kotlin
  3. Creating and Monitoring Geofences

我继承了一个Java应用程序,所以#2对我不起作用。在#3中,我有一些构建失败,直到我将以下内容添加到我的gradle.properties文件中:

android.useAndroidX=true
android.enableJetifier=true

随着构建工作,我尝试使用Pixel 2 API 28在模拟器上运行应用程序。我登录了Play商店,切换了“Google Location Accuracy”ON,启用了Wifi和蓝牙扫描,并收到了以下日志:

2019-01-14 23:38:43.597 25221-25221/? I/mple.geofencin: Not late-enabling -Xcheck:jni (already on)
2019-01-14 23:38:43.636 25221-25221/? W/mple.geofencin: Unexpected CPU variant for X86 using defaults: x86
2019-01-14 23:38:44.134 25221-25221/com.google.android.gms.location.sample.geofencing W/mple.geofencin: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-01-14 23:38:44.135 25221-25221/com.google.android.gms.location.sample.geofencing W/mple.geofencin: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-01-14 23:38:44.328 25221-25221/com.google.android.gms.location.sample.geofencing I/MainActivity: Requesting permission
2019-01-14 23:38:44.393 25221-25221/com.google.android.gms.location.sample.geofencing D/OpenGLRenderer: Skia GL Pipeline
2019-01-14 23:38:44.470 25221-25254/com.google.android.gms.location.sample.geofencing I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-01-14 23:38:44.470 25221-25254/com.google.android.gms.location.sample.geofencing I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2019-01-14 23:38:44.471 25221-25254/com.google.android.gms.location.sample.geofencing I/OpenGLRenderer: Initialized EGL, version 1.4
2019-01-14 23:38:44.471 25221-25254/com.google.android.gms.location.sample.geofencing D/OpenGLRenderer: Swap behavior 1
2019-01-14 23:38:44.471 25221-25254/com.google.android.gms.location.sample.geofencing W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-01-14 23:38:44.472 25221-25254/com.google.android.gms.location.sample.geofencing D/OpenGLRenderer: Swap behavior 0
2019-01-14 23:38:44.484 25221-25254/com.google.android.gms.location.sample.geofencing D/EGL_emulation: eglCreateContext: 0xe14051e0: maj 3 min 0 rcv 3
2019-01-14 23:38:44.505 25221-25254/com.google.android.gms.location.sample.geofencing D/EGL_emulation: eglMakeCurrent: 0xe14051e0: ver 3 0 (tinfo 0xe1403660)
2019-01-14 23:38:44.679 25221-25254/com.google.android.gms.location.sample.geofencing D/EGL_emulation: eglMakeCurrent: 0xe14051e0: ver 3 0 (tinfo 0xe1403660)
2019-01-14 23:38:47.961 25221-25221/com.google.android.gms.location.sample.geofencing I/MainActivity: onRequestPermissionResult
2019-01-14 23:38:47.961 25221-25221/com.google.android.gms.location.sample.geofencing I/MainActivity: Permission granted.
2019-01-14 23:38:53.437 25221-25254/com.google.android.gms.location.sample.geofencing D/EGL_emulation: eglMakeCurrent: 0xe14051e0: ver 3 0 (tinfo 0xe1403660)
2019-01-14 23:38:53.442 25221-25254/com.google.android.gms.location.sample.geofencing D/EGL_emulation: eglMakeCurrent: 0xe14051e0: ver 3 0 (tinfo 0xe1403660)
2019-01-14 23:40:18.152 25221-25241/com.google.android.gms.location.sample.geofencing I/AssistStructure: Flattened final assist data: 1928 bytes, containing 1 windows, 16 views
2019-01-14 23:40:20.314 25221-25254/com.google.android.gms.location.sample.geofencing D/EGL_emulation: eglMakeCurrent: 0xe14051e0: ver 3 0 (tinfo 0xe1403660)

即使在模拟器中将位置更改为SFO / GOOGLE之后,模拟器上也没有通知。我在onHandleWorkGeofenceTransitionsJobIntentService中添加了一个打印声明,但看起来没有发生地理围栏事件。

我也尝试过使用假GPS的真正Pixel设备;也没有通知。那么,我的问题是我们应该为Geofencing使用哪些示例代码?它应该在模拟器上工作吗?如果我将应用程序切换到后台以更改假GPS应用程序中的位置,它是否适用于真实手机?

PS:我的模拟器图像启用了Google Play PPS:我也试过了这个XYZ旅游景点应用程序;当我通过模拟器更改GPS位置但仍然没有通知时,我可以看到距离发生变化。虽然我没有深入研究这个问题。 PPPS:请帮忙!

更新1:

  • 我确实收到地理围栏过渡事件..但只有谷歌地图在前台运行。我不确定这是因为我是通过模拟器设置位置的...
  • (3)中的构建中断似乎是5天前更新的googlesamples repo中的回归。提交432d3b72b8c058f220416958b444274ddd186abd似乎开箱即用。
android android-studio android-location android-geofence
1个回答
2
投票

我已经和GeoFence合作了这么长时间,我有同样的问题,在尝试不同的解决方案之后我自己得到了答案。所以基本上,如果手机中的任何应用程序在某个x持续时间内获取该位置,GeoFence只会获得触发器。如果您测试谷歌提供的GeoFence示例应用程序,那么您可以看到该应用程序仅在您打开Goog​​le地图应用程序时有效,因为Google地图是设备中被动地请求位置的唯一应用程序。

对于Prove,您可以从以下链接克隆GeoFence示例和LocationUpdateForGroundService示例链接https://github.com/googlesamples/android-play-location同时运行它们GeoFence和LocationUpdateForGroundService,您将注意到通过更改模拟器中的lat和long,现在您不需要打开Goog​​le地图任何更多,因为现在有另一个应用程序请求位置。

因此,请在GeoFence应用程序中创建前台服务,并使用Fuse Location Client请求某个x持续时间的位置更新。

© www.soinside.com 2019 - 2024. All rights reserved.