-1
我有一個GeofenceImplementorClass。我在其中添加了三個地理柵欄,其中一個是我從獲得的當前位置。我期待IntentClass服務得到執行,因爲我已經在一個地理圍欄位置,但是服務沒有得到執行。請幫忙。Android Geofencing PendingIntent not firing
public class GeofenceImplementor implements ResultCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
private ArrayList<Geofence> geofenceList = new ArrayList<>();
private PendingIntent pendingIntent = null;
private GoogleApiClient googleApiClient;
private Boolean isConnected = false;
private Boolean request = false;
private Context context;
public GeofenceImplementor(Context context){
this.context = context;
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
public void addToList(LatLng latLng,String key){
geofenceList.add(new Geofence.Builder()
.setRequestId(key)
.setCircularRegion(latLng.latitude, latLng.longitude, 1000)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(geofenceList);
return builder.build();
}
private PendingIntent getPendingIntent(){
if(pendingIntent != null)
return pendingIntent;
Intent intent = new Intent(context,IntentClass.class);
return PendingIntent.getService(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
public void implement() {
if(isConnected) {
try {
LocationServices.GeofencingApi.addGeofences(googleApiClient,
getGeofencingRequest(), getPendingIntent()).setResultCallback(this);
} catch (SecurityException ex) {
Log.e("Security Exception", ex.getStackTrace().toString());
}
}
else request = true;
}
}
你是什麼意思的「當前位置半徑」? –
設備上當前位置周圍出現的圓的半徑。 – user1232138