我正在使用内置的地理围栏API(播放服务),并且结果好坏参半.看起来在设置地理围栏之后,即使GPS打开,进入/退出的通知也非常不一致,具有最新位置(连接了LocationClient,在后台运行.)我开始监控(轮询)位置变化和调试文本字段中的距离,并且即使基于设备注册的位置和地理围栏的位置(我在技术上是在地理围栏的内部/外部),通常有时会触发通知,有时也不会.有什么方法可以让它更具可预测性?我几乎想要放弃这个api并根据设备位置的轮询实现我自己的(电池耗尽)地理围栏.
我正在编写一个应用程序,当有人在正在安装的应用程序的生命周期内进入/退出多个站点时,需要使用地理围栏.
我的地理围栏实现(非常类似于下面的第二个链接)在我第一次安装应用程序时工作正常,无论是在移入/移出地理围栏时还是在使用模拟位置进行模拟时,都会重新启动设备.
在重新启动时,模拟位置或实际上物理移入和移出地理围栏似乎都会触发事件并将未决意图发送到我的广播接收器.
我已经查看了以下三个链接,并且还阅读了相当多的文档,但我无法找到一个明确的答案,直接说注册地理围栏持续存在或重启后不会持续存在.
这些是我在堆栈溢出时查看的链接: Android地理围栏是否能够在重启时幸存?
Android Geofences是否在删除/过期之前保持活动状态,或者直到我的PendingIntent启动为止
如果有人碰巧知道他们是否坚持重新启动后的答案,或者如果他们没有解决问题,那将非常感谢!我目前的最后一个希望是为BOOT_COMPLETED创建一个监听器,并在启动时重新注册它们,但id更喜欢只在必要时才这样做.
非常感谢提前!
编辑:虽然我没有找到明确的(书面)答案,但我很确定TonyC先生发布的内容是正确的,并选择了解决方案.非常感谢TonyC!
如果有人想看到我的解决方案,我会在设备启动时监听启动完成操作,然后重新注册我需要的所有地理围栏.
这是显而易见的:
<!-- Listen for the device starting up -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="com.YOUR.PACKAGE.geofence.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
然后为它创建一个广播接收器,它将在启动时重新注册地理围栏:
package com.YOUR.PACKAGE.geofence;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
public class BootCompleteReceiver extends WakefulBroadcastReceiver
{
private static final String TAG = "BootCompleteReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
//Do what you want/Register Geofences
}
}
Run Code Online (Sandbox Code Playgroud)
同样值得注意的是,如果您在启动时处于地理围栏内,那么一旦注册了地理围栏,这通常会触发地理围栏的未决意图.
因此,例如,如果地理围栏启动应用程序,那么当您启动恰好位于地理围栏中的设备时,一旦启动完整广播接收器已注册地理围栏,它也将打开应用程序,并且位置服务已经解决了您的位置是.
希望对某人有所帮助.
android geofencing google-play-services android-geofence location-client
如果我有这两个地理围栏,在注册这些地理围栏之后,当我进入或退出这些圈子的圆周时,我会得到通知.但是,如果我正在通过公共区域,即从一圈到另一圈,我不希望我的应用程序发送通知.
可能吗?如果是这样,那怎么样?

我刚刚完成了Android上的地理围栏教程(http://developer.android.com/training/location/geofencing.html),我想知道为什么地理围栏的'回调'是通过挂起的意图而不是简单的回调接口完成的.
如果在活动中实现,通常会断开位置客户端的连接onPause(),因此在应用程序暂停/被销毁后,以前添加的地理围栏也不会被跟踪,那么为什么会有未决的意图呢?或者我错了?
我正在尝试使用Google的示例测试地理围栏功能:创建和监控地理围栏.我在这里上传了代码.
问题是我从未收到进入或退出的通知.我有Wifi,4G和GPS.我试过用以下方法测试:
我究竟做错了什么?ANyone成功运行了这个谷歌的例子:创建和监控地理围栏.我在这里上传了代码以便于浏览.
我只是想学习Geofencing - 检测进入和退出.我将标记任何答案作为我可以用来在我的真实设备上进行地理工作的正确答案
.
I'm doing a serious research on this topic for many days... I saw many topics here too... But unfortunately I couldn't find a solution....
I'm writing an app that uses the new Google API for Geofence... Well, I can handle "ins" and "outs" of a geofence, but only if my app is open! Even if I'm with wifi on, gps on, and 3G on, but the app, it does not trigger any event...Just if the app is open...
I'm using …
我已经使用了Geofences的Android教程,显然,当app关闭时,它不起作用.所以,在搜索了之后,我注意到SO上的用户b-ryce使用了BroadcastReceiver,因此即使应用程序不活动也会触发地理围栏(链接为他的SO问题).
当我移动到外部/内部注册位置时,我无法强制地理围栏触发.这是我的程序:
/**
* GeoLocation library
*/
mGeoLocation = new GeoLocation(sInstance);
/**
* mReceiver init
*/
mReceiver = new Receiver();
sInstance.registerReceiver(mReceiver, mReceiver.createIntentFilter());
Run Code Online (Sandbox Code Playgroud)
在GeoLocation类中:
/*
* Create a PendingIntent that triggers an IntentService in your
* app when a geofence transition occurs.
*/
protected PendingIntent getTransitionPendingIntent() {
if (mGeoPendingIntent != null) {
return mGeoPendingIntent;
}
else {
// Create an explicit Intent
// Intent intent = new Intent(mContext,
// ReceiveTransitionsIntentService.class);
Intent intent = new Intent(getClass().getPackage().getName() + ".GEOFENCE_RECEIVE");
/**
* …Run Code Online (Sandbox Code Playgroud) 我的主要要求是让服务拥有自己的流程并触发自己的地理围栏事件。我希望用户在进入地理围栏时收到通知中心的通知,即使应用程序被终止。
我读过服务,这对我来说似乎很清楚,Android 文档很密集,所以我设法了解如何使用自己的进程启动服务,以及它如何使用 Messenger 与应用程序通信。
然后是来自 Google 的此代码示例,展示了如何将地理围栏与 google play 服务一起使用: Google 示例地理围栏
到目前为止,我发现我们必须使用 IntentService 来触发地理围栏事件,并且从我读过的文档中可以看出,IntentService 在其工作完成后会自行终止。而且我也进行了一些测试,看起来 Intentservice 在应用程序被杀死时被杀死,这对文档来说并不让我感到惊讶。
所以我的问题是,我们如何执行具有自己流程的地理围栏服务?在这件事上可以避免使用 IntentService 吗?
编辑:
我想这样做的原因是用户可以设置地理围栏“提醒”,它基于日常使用。例如,假设您每天到达工作场所,当我们进入此地理围栏时,我们应该能够发出警报(或通知),以了解应用程序是否正在运行。我们不希望用户担心应用程序在后台运行。
我正试图Geofence在我的APP 上实现,问题是当我在我标记的地方时不会触发.
我做的是我从地方选择器设置Latitude&.Longitude
我得到的价值很好,因为我把它放在google maps正确的位置,因为我已经阅读了许多人从lat/long设置错误值的答案.
我有两个班:GeofenceSingleton.class和GeofenceTransitionsIntentService.
的GeofenceSingleton.class样子:
public class GeofenceSingleton implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {
private GoogleApiClient googleApiClient;
private static GeofenceSingleton _singleInstance;
private static Context appContext;
private static final String ERR_MSG = "Application Context is not set!! " +
"Please call GeofenceSngleton.init() with proper application context";
private PendingIntent mGeofencePendingIntent;
private static ArrayList<Geofence> mGeofenceList;
private GeofenceSingleton() {
if (appContext == null)
throw new IllegalStateException(ERR_MSG);
this.googleApiClient …Run Code Online (Sandbox Code Playgroud) 当我尝试在我的应用程序中使用地理围栏时,我遇到了问题.我已成功注册地理围栏,但我从未收到通知.
首先,我按照http://developer.android.com/training/location/geofencing.html中提供的示例进行操作,但即使我走向或远离区域,代码也不会触发意向服务.
我的注册地理围栏代码和调用意图服务如下:
内部清单:
<service
android:name="com.example.zukami.apps.blynk.geofence.ReceiveTransitionsIntentService"
android:exported="true" >
</service>
Run Code Online (Sandbox Code Playgroud)
在我的MainActivity中
private boolean registerGeofence() {
mRequestType = GeofenceUtils.REQUEST_TYPE.ADD;
if (!servicesConnected()) {
return false;
}
SimpleGeofence testGeo = new SimpleGeofence(Flag.GEOFENCE + "_"
+ 1, Double.valueOf("1.317342"),
Double.valueOf("103.841998"), (float) (200),
GEOFENCE_EXPIRATION_IN_HOURS,
Geofence.GEOFENCE_TRANSITION_ENTER);
mPrefs.setGeofence(Flag.GEOFENCE + "_" + 1, testGeo);
mCurrentGeofences.add(testGeo.toGeofence());
SimpleGeofence testGeo2 = new SimpleGeofence(Flag.GEOFENCE + "_"
+ 2, Double.valueOf("1.303961"),
Double.valueOf("103.909356"), (float) (200),
GEOFENCE_EXPIRATION_IN_HOURS,
Geofence.GEOFENCE_TRANSITION_ENTER);
mPrefs.setGeofence(Flag.GEOFENCE + "_" + 2, testGeo2);
mCurrentGeofences.add(testGeo2.toGeofence());
// end deleted this code after testing
try {
mGeofenceRequester.addGeofences(mCurrentGeofences);
Log.e(TAG, …Run Code Online (Sandbox Code Playgroud) android ×10
geofencing ×6
android-maps ×1
geolocation ×1
google-maps ×1
gps ×1
location ×1
service ×1