2015-05-27 136 views
2

我已使用Fused Location Provider API获取用户的当前位置。当用户打开位置时,该功能可以很好地工作。如何跟踪用户是否有意关闭位置,使用熔合位置提供程序API

但是我想提供一个消息给用户,如果他已经关闭了位置。当位置关闭时,requestLocationUpdates不会调用onLocationChanged方法。可能这是预期的,但我想知道什么是所谓的位置关闭,以便我可以捕获它通知用户。以下是我正在使用的代码(从Stackoverflow中的示例中摘取,其中的链接已从我中删除)。

public class FusedLocationService implements 
    LocationListener, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 

private static final String TAG = "FusedLocationService"; 

private static final long INTERVAL = 1000 * 30; 
private static final long FASTEST_INTERVAL = 1000 * 5; 
private static final long ONE_MIN = 1000 * 60; 
private static final long REFRESH_TIME = ONE_MIN * 5; 
private static final float MINIMUM_ACCURACY = 50.0f; 
Activity locationActivity; 
private LocationRequest locationRequest; 
private GoogleApiClient googleApiClient; 
private Location location; 
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi; 

FusedLocationReceiver locationReceiver = null; 

public FusedLocationService(Activity locationActivity, FusedLocationReceiver locationReceiver) { 
    this.locationReceiver = locationReceiver; 
    locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    locationRequest.setInterval(INTERVAL); 
    locationRequest.setFastestInterval(FASTEST_INTERVAL); 
    this.locationActivity = locationActivity; 

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

    if (googleApiClient != null) { 
     googleApiClient.connect(); 
    } 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    Log.i(TAG, "I'm connected now"); 
    Location currentLocation = fusedLocationProviderApi.getLastLocation(googleApiClient); 
    if (currentLocation != null && currentLocation.getTime() > REFRESH_TIME) { 
     location = currentLocation; 
    } else { 
     fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
     // Schedule a Thread to unregister location listeners 
     Executors.newScheduledThreadPool(1).schedule(new Runnable() { 
      @Override 
      public void run() { 
       fusedLocationProviderApi.removeLocationUpdates(googleApiClient, 
         FusedLocationService.this); 
      } 
     }, ONE_MIN, TimeUnit.MILLISECONDS); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.i(TAG, "Location is changed!"); 
    //if the existing location is empty or 
    //the current location accuracy is greater than existing accuracy 
    //then store the current location 
    if (null == this.location || location.getAccuracy() < this.location.getAccuracy()) { 
     this.location = location; 
// let's inform my client class through the receiver 
     locationReceiver.onLocationChanged(); 
     //if the accuracy is not better, remove all location updates for this listener 
     if (this.location.getAccuracy() < MINIMUM_ACCURACY) { 
      fusedLocationProviderApi.removeLocationUpdates(googleApiClient, this); 
     } 
    } 
} 

public Location getLocation() { 
    return this.location; 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

} 

回答

1

我想你可以只使用LocationManager每一个问题:How to check if Location Services are enabled?

+0

我用http://stackoverflow.com/a/22980843/954010跟踪位置。希望这可以在所有设备中可靠运行。谢谢 – maya

+0

该解决方案已有3年历史。现在GooglePlayServices发生了很多变化。只是想你可能知道。 –

+0

根据当前的API,该解决方案仍然有效,并且对于我工作的内容来说,它非常有效。 –

相关问题