Android GeoFenging API广播接收器未触发
我试图使用google play服务中的GeoFenging api,但我无法设法让广播接收器触发其Android GeoFenging API广播接收器未触发,android,broadcastreceiver,google-play-services,geofencing,location-client,Android,Broadcastreceiver,Google Play Services,Geofencing,Location Client,我试图使用google play服务中的GeoFenging api,但我无法设法让广播接收器触发其onReceive()方法 看起来我尊重一切需要使它工作,但我没有得到任何过渡事件 我将google play服务包括在我的依赖项中 我请求ACCESS\u FINE\u LOCATION权限 我在清单中注册我的BroadcastReceiver,将导出的值设置为“true”(有人说这是必需的) 我在我的确切位置加了一个大的地理围栏(我用手机上的谷歌地图验证了它),既有转换类型也没有过期 我尝试
onReceive()
方法
看起来我尊重一切需要使它工作,但我没有得到任何过渡事件
- 我将google play服务包括在我的依赖项中
- 我请求
权限ACCESS\u FINE\u LOCATION
- 我在清单中注册我的
,将导出的值设置为“true”(有人说这是必需的)BroadcastReceiver
- 我在我的确切位置加了一个大的地理围栏(我用手机上的谷歌地图验证了它),既有转换类型也没有过期 <>我尝试了一个精确的模拟位置在GeFONCE的中间,另一个在GeFONCE中10秒之后(并且我确定了<代码> Access MoCkl位置< /代码>提交)。
- 我验证了我的手机的位置是开着的,并且它的模式被设置为高精度
- 我验证了网络连接
- 在添加geofence后,我尝试了使用和不使用LocationClient断开连接
onAddGeofencesResult()
回调,但没有得到onReceive()
回调。我用Nexus5和Nexus7试过,但都没能触发geofence转换
我也试过这个样本,但它的行为是一样的
这是我的BroadcastReceiver
课程:
public class GeofencesReceiver extends BroadcastReceiver implements ConnectionCallbacks, OnConnectionFailedListener, OnAddGeofencesResultListener {
private final static String TAG = "GeofencesReceiver";
private Context context = null;
private LocationClient locationClient = null;
/**
* An empty constructor is needed by the manifest.
*/
@SuppressWarnings("unused")
public GeofencesReceiver(){}
public GeofencesReceiver(Context context){
this.context = context;
locationClient = new LocationClient(context, this, this);
locationClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
new Thread(new Runnable() {
@Override
public void run() {
List<Geofence> geofences = getGeofences();
if(geofences.size() > 0){
Intent geofenceBroadcastReceiverIntent = new Intent(context, GeofencesReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceBroadcastReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.addGeofences(geofences, pi, GeofencesReceiver.this);
}else
Log.w(TAG, "No GPS zone found");
}
}).start();
}
private List<Geofence> getGeofences(){
Vector<Geofence> geofences = new Vector<Geofence>();
Geofence geofence = new Geofence.Builder()
.setRequestId("1")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(45.2676018, -72.1572185, 100)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
geofences.add(geofence);
return geofences;
}
@Override
public void onDisconnected() {
Log.d(TAG, "onDisconnected");
}
@Override
public void onAddGeofencesResult(int i, String[] strings) {
if(i != LocationStatusCodes.SUCCESS)
Log.e(TAG, "Failed to add geofences");
else
Log.d(TAG, "Geofences successfully added");
locationClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed");
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
if(LocationClient.hasError(intent)){
int errorCode = LocationClient.getErrorCode(intent);
Log.e(TAG, "Location Services error: " + Integer.toString(errorCode));
return;
}
int transitionType = LocationClient.getGeofenceTransition(intent);
if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
for(Geofence geofence : triggerList){
Log.d(TAG, (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ? "Entering" : "Exiting") + " GPS zone " + geofence.getRequestId());
}
}else{
Log.e(TAG, "Geofence transition error: " + transitionType);
}
}
}
我花了一整天的时间想弄明白为什么它不起作用。。。从Logcat我找到了这些行,但我不确定它们是否与问题有关,因为我在谷歌上没有找到关于地理围栏错误的帖子
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Not calling LocationReportingService, hasMoved: true, elapsed millis: 580166, request: Phone
04-07 09:02:16.481 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:02:16.501 1160-1319/? I/GCoreUlr﹕ Starting service, intent=Intent { cmp=com.google.android.gms/com.google.android.location.reporting.LocationReportingService }, extras=null
04-07 09:02:16.571 1210-1252/? W/GLSUser﹕ GoogleAccountDataService.getToken()
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Failed write_ctrl(u 69) res=-1 errno=22
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Untagging socket 69 failed errno=-22
04-07 09:02:16.601 1160-3780/? W/NetworkManagementSocketTagger﹕ untagSocket(69) failed with errno -22
04-07 09:02:16.601 1160-3780/? I/imp﹕ I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
04-07 09:02:16.601 1160-3780/? I/imp﹕ Retrying request
04-07 09:02:17.501 1160-6156/? I/GCoreUlr﹕ Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_ACTIVE_STATE cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService }, extras=null
04-07 09:02:17.511 1160-6156/? I/GCoreUlr﹕ Batch Location Update succeeded for account account#7#
04-07 09:02:17.571 1160-1319/? I/GCoreUlr﹕ Ensuring that reporting is active for [account#7#]
如果没有人能帮助我,我担心我会用在GPS\U提供商注册的LocationManager实现我自己的地理围栏算法,这会耗尽我用户的电池…首先,你确定你的坐标是正确的吗?在玩地理围栏时,我遇到了一个非常类似的问题,我花了一段时间才意识到我的坐标只是颠倒了
不用说,我觉得自己很愚蠢 你在模拟gps定位吗?我想说,土工围栏的精度不可靠,我也尝试过模拟位置。我在地理位置中间使用了一个精度很小的位置。这与我使用真实位置时完全一样。只有当你离开地理围栏时,事件才会触发我在地理围栏内使用了模拟位置,10秒后,在非常远处,但仍然没有触发事件…:(嗯…今天每次我启动我的应用程序,它都会工作,我没有修改手机的设置,也没有修改我的应用程序代码…我不明白这个地理围栏API-u-是的,我刚刚意识到测试我的东西的家伙输入了反向坐标。我肯定应该在我的代码中添加更多日志。。。
dependencies {
compile 'com.google.android.gms:play-services:4.3.23'
}
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Not calling LocationReportingService, hasMoved: true, elapsed millis: 580166, request: Phone
04-07 09:02:16.481 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:02:16.501 1160-1319/? I/GCoreUlr﹕ Starting service, intent=Intent { cmp=com.google.android.gms/com.google.android.location.reporting.LocationReportingService }, extras=null
04-07 09:02:16.571 1210-1252/? W/GLSUser﹕ GoogleAccountDataService.getToken()
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Failed write_ctrl(u 69) res=-1 errno=22
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Untagging socket 69 failed errno=-22
04-07 09:02:16.601 1160-3780/? W/NetworkManagementSocketTagger﹕ untagSocket(69) failed with errno -22
04-07 09:02:16.601 1160-3780/? I/imp﹕ I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
04-07 09:02:16.601 1160-3780/? I/imp﹕ Retrying request
04-07 09:02:17.501 1160-6156/? I/GCoreUlr﹕ Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_ACTIVE_STATE cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService }, extras=null
04-07 09:02:17.511 1160-6156/? I/GCoreUlr﹕ Batch Location Update succeeded for account account#7#
04-07 09:02:17.571 1160-1319/? I/GCoreUlr﹕ Ensuring that reporting is active for [account#7#]