我有一个以Google的地理围栏示例代码开头的应用.它可以工作几天,我得到了所有过渡意图,正如我预期的那样.然而,经过一段时间,比如3天,应用程序停止了这些意图,我不知道为什么.
当我创建我的围栏时,我将到期时间设置为Geofence.NEVER_EXPIRE
这是我的IntentService,我在它停止工作之前获得转换意图:
public class ReceiveTransitionsIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
Intent broadcastIntent = new Intent();
broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
// First check for errors
if (LocationClient.hasError(intent)) {
...handle errors
} else {
// Get the type of transition (entry or exit)
int transition = LocationClient.getGeofenceTransition(intent);
// Test that a valid transition was reported
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
|| (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {
// Post a notification
NEVER GETS HERE
} else {
...log error
}
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Google的示例测试地理围栏功能:创建和监控地理围栏.我在这里上传了代码.
问题是我从未收到进入或退出的通知.我有Wifi,4G和GPS.我试过用以下方法测试:
我究竟做错了什么?ANyone成功运行了这个谷歌的例子:创建和监控地理围栏.我在这里上传了代码以便于浏览.
我只是想学习Geofencing - 检测进入和退出.我将标记任何答案作为我可以用来在我的真实设备上进行地理工作的正确答案
.
我已经使用了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)