Rap*_*ard 11 android broadcastreceiver geofencing google-play-services location-client
我正在尝试使用Google Play服务中的geofencing api,但我无法设法让我的广播接收器触发其onReceive()方法.
看起来我尊重使其工作所需的一切,但我没有过渡事件.
ACCESS_FINE_LOCATION许可BroadcastReceiver在我的Manifest中注册了我的导出值设置为"true"(有人说它是必需的)ACCESS_MOCK_LOCATION存在延迟)但是,我仍然获得了成功的onAddGeofencesResult()回调,但没有onReceive()回调.我尝试使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的清单中,我有以下几行(不在该结构中)
<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" />
Run Code Online (Sandbox Code Playgroud)
在我的build.gradle文件中,我包含了这样的服务
dependencies {
compile 'com.google.android.gms:play-services:4.3.23'
}
Run Code Online (Sandbox Code Playgroud)
我花了一整天的时间试图找出它为什么不起作用......从Logcat我发现了这些行,但我不确定它们是否与问题有关,因为我在Google上找不到关于这些错误的帖子与地理围栏.
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#]
Run Code Online (Sandbox Code Playgroud)
如果没有人可以帮助我,我担心我将使用在GPS_PROVIDER注册的LocationManager实现我自己的地理围栏算法,这将耗尽用户的电池......
| 归档时间: |
|
| 查看次数: |
3940 次 |
| 最近记录: |