我正在处理Geofencing
申请。处理JobIntentService
的{{1}}子类永远不会收到意图。我以一分钟的间隔收到位置更新,然后创建新的地理围栏列表,然后根据用户的当前位置添加地理围栏。
这是我的GeofenceTransitions
AndroidManifest.xml
我的 ........
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service
android:name=".GeofenceTransitionsService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE">
</service>
<receiver
android:name=".GeofenceBroadcastReceiver"
android:enabled="true"
android:exported="true" />
.............
GeofenceBroadcastReceiver.java
我的public class GeofenceBroadcastReceiver extends BroadcastReceiver {
/**
* Receives incoming intents.
*
* @param context the application context.
* @param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
public void onReceive(Context context, Intent intent) {
// Enqueues a JobIntentService passing the context and intent as parameters
GeofenceTransitionsService.enqueueWork(context, intent);
}
}
处理触发的地理围栏
GeofenceTransitionsService.java
我public class GeofenceTransitionsService extends JobIntentService {
..........
/**
* Convenience method for enqueuing work in to this service
* Enqueue new work to be dispatched to onHandleWork
*/
public static void enqueueWork(Context context, Intent intent) {
Log.d(TAG, "Received intent: " + intent);
enqueueWork(context, GeofenceTransitionsService.class, JOB_ID, intent);
}
@Override
protected void onHandleWork(Intent intent){
// We have received work to do. The system or framework is already
// holding a wake lock for us at this point, so we can just go.
Log.d(TAG, "Received intent: " + intent);
}
}
中代码的一部分,它创建了一个地理围栏请求,创建了待处理的意图并添加了地理围栏
PointOfInterestMapFragment.java
这是 /* Use the GeofencingRequest class and its nested GeofencingRequestBuilder
* class to specify the geofences to monitor and to set how related geofence events are
* triggered
*/
private GeofencingRequest getGeofencingRequest(){
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
//tell Location services that GEOFENCE_TRANSITION_DWELL should be triggered if the
//device is already inside the geofence
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}//end method getGeofencingRequest
/*Pending intent that starts the IntentService*/
private PendingIntent getGeofencePendingIntent(){
Log.d(TAG, "getPendingIntent()");
//Reuse the pending intent if we already have it
if(mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(getActivity(), GeofenceBroadcastReceiver.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
mGeofencePendingIntent = PendingIntent.getBroadcast(getActivity()
, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}//end method PendingIntent
/*Add geofences*/
@SuppressWarnings("MissingPermission")
private void addGeofence(){
if(checkPermissions()){
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Geofence added");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Failed to add geofence: " + e.getMessage());
}
})
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
//drawGeofence();
}
});
}else{
requestPermissions();
}
}//end method addGeofence
中我收到位置更新的代码部分,填充PointOfInterestMapFragment.java
然后添加地理围栏
GeofenceList
当应用程序执行时,我从/**
* Creates a callback for receiving location events.
*/
private void createLocationCallback() {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
mCurrentLocation = locationResult.getLastLocation();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
//populateGeofenceList to reflect the new current location bounds
populateGeofenceList();
addGeofence();
}
};
}
中的代码行Log.d(TAG, "getPendingIntent()");
获取log cat中的消息,但从未得到应该在getGeofencePendingIntent()
方法中显示的消息
答案 0 :(得分:0)
我遇到了类似的问题&#39;。代码很好。在我的情况下,我认为代码不起作用,因为我没有正确理解地理围栏是如何工作的。我想添加地理围栏,在你的情况下,对addGeofence()
的调用是触发器,所以我等着在那个特定时间点看到通知。但是,对该方法的调用只会添加监控的地理围栏,然后只有在满足任何过滤器时才会将意图传递给服务(Geofence.GEOFENCE_TRANSITION_DWELL
,Geofence.GEOFENCE_TRANSITION_EXIT
或{ {1}})增加了地理围栏。您可以阅读文档here
因此,您可能会在日志中收到 Geofence添加消息,但这就是字面意思,地理位置已添加而未触发。添加地理围栏后等待一段时间,如果对添加的地理围栏满足任何过滤器,则会发送意图。所以对我有用的解决方案是等待,我在一段时间后收到了意图和通知。
如果等待不起作用,您可能需要延长Geofence.GEOFENCE_TRANSITION_ENTER
,比如检查3000米检查是否有任何变化。另外,将到期时间设置为更高的值或GEOFENCE_RADIUS