我试图使用谷歌Play服务的地理围栏API,但我无法让广播接收器触发其onReceive()
方法。
尽管我已经完成了所有必要的步骤,但我没有得到任何过渡事件。
- 我在依赖项中包含了Google Play服务
- 我请求了
ACCESS_FINE_LOCATION
权限 - 我在清单文件中将我的
BroadcastReceiver
注册为导出值设置为"true"(有些人说这是必须的) - 我在精确定位的位置添加了一个大型地理围栏(在我的手机上通过Google Maps验证),同时使用了两种转换类型和不过期
- 我使用了一个精确的模拟位置,在地理围栏的正中心,另一个在地理围栏之外10秒钟之后(我确保有
ACCESS_MOCK_LOCATION
权限) - 我验证了我的手机位置已经开启并且精度设置为高精度
- 我验证了互联网连接
- 我在添加地理围栏后尝试断开LocationClient并重新连接
但仍然无法触发地理围栏转换,虽然我得到了成功的onAddGeofencesResult()
回调。 我使用Nexus 5和Nexus 7都无法触发地理围栏转换。
我还尝试了https://developer.android.com/training/location/geofencing.html上的示例,但其行为相同。
这是我的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);
}
}
}
在我的清单文件中,我有以下几行(不一定是这个结构)。<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name="novom.anyware.anywaresdk.GeofencesReceiver" android:exported="true" />
<meta-data android:name="com.google.android.gms.version" android:value="4323000" />
在我的 build.gradle 文件中,我是这样包含服务的
dependencies {
compile 'com.google.android.gms:play-services:4.3.23'
}
我花了整整一天的时间试图弄清楚为什么它不起作用... 从Logcat中,我找到了这些行,但我不确定它们是否与问题有关,因为我在Google上没有找到任何有关使用Geofencing时出现这些错误的帖子。
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_PROVIDER上的LocationManager实现自己的地理围栏算法,并且这将消耗我的用户的电池寿命...