GeofenceIntentService未在地理围栏输入时触发

时间:2017-12-28 15:58:33

标签: android service location android-geofence

我试图在没有谷歌地图的情况下创建地理围栏(只是当用户在地理围栏区域时触发PendingIntent的服务)。问题是,当用户进入地理围栏位置时,我无法得到响应。这是我的服务类代码:

public class GeofenceBackgroundService extends Service implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener,
    ResultCallback<Status> {

private GoogleApiClient googleApiClient;
private GeofencingClient mGeofencingClient;
private LocationRequest locationRequest;
private PendingIntent geoFencePendingIntent;
private final int GEOFENCE_REQ_CODE = 0;
private static final String GEOFENCE_REQ_ID = "My Geofence";
private static final float GEOFENCE_RADIUS = 500.0f;


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    /*mGeofencingClient = LocationServices.getGeofencingClient(this);*/

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    googleApiClient.connect();

    locationRequest = LocationRequest.create()
            .setInterval(10000)
            .setFastestInterval(5000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    return START_STICKY;
}


@Override
public void onConnected(Bundle dataBundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);

    Geofence geofence = createGeofence();
    GeofencingRequest geofenceRequest = createGeofenceRequest(geofence);
    addGeofence(geofenceRequest);
}

@Override
public void onConnectionSuspended(int i) {
    googleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

@Override
public void onLocationChanged(Location location) {

    if (location != null) {
        Log.e("TAG", "Location changed " + location.getLatitude() +" " + location.getLongitude());
    }
}

private Geofence createGeofence() {
    return new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion(35.3140045, 103.4839695, GEOFENCE_RADIUS)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setNotificationResponsiveness(1000)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();
}

private GeofencingRequest createGeofenceRequest(Geofence geofence) {
    return new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofence(geofence)
            .build();
}

private PendingIntent createGeofencePendingIntent() {
    if (geoFencePendingIntent != null)
        return geoFencePendingIntent;
    /*Intent intent = new Intent("com.receive.ACTION_RECEIVE_GEOFENCE");
    PendingIntent geoFencePendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    return geoFencePendingIntent;*/
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, GEOFENCE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}


private void addGeofence(GeofencingRequest request) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.GeofencingApi.addGeofences(googleApiClient, request, createGeofencePendingIntent()).setResultCallback(this);
}

@Override
public void onResult(@NonNull Status status) {
    if ( status.isSuccess() ) {
    } else {
    }
}


@Override
public void onDestroy() {
    super.onDestroy();
    googleApiClient.disconnect();
}}

此外,我尝试使用IntentService和Broadcast接收器并在android清单文件中正确声明它们

0 个答案:

没有答案