2017-01-31 2 views
0

Я хочу получить местоположение в фоновом режиме, чтобы получить местоположение из любой активности/фрагмента. проблема в том, что я не получаю широту и долготу в сервисе. Что-то я здесь делаю неправильно?получить местоположение с помощью GoogleAPIClient в службе

public class LocationService extends Service implements 
 
     GoogleApiClient.ConnectionCallbacks, 
 
     GoogleApiClient.OnConnectionFailedListener, 
 
     LocationListener { 
 

 
    private static final String TAG = "sammy_"; 
 
    private boolean currentlyProcessingLocation = false; 
 
    private LocationRequest locationRequest; 
 
    private Location mCurrentLocation; 
 
    private GoogleApiClient googleApiClient; 
 

 
    @Override 
 
    public void onCreate() { 
 
     super.onCreate(); 
 
    } 
 

 

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

 
     return START_NOT_STICKY; 
 
    } 
 

 
    private void startTracking() { 
 
     Log.d(TAG, "startTracking");  
 

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

 
      
 
     
 
    } 
 

 

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

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

 
    @Override 
 
    public void onLocationChanged(Location location) { 
 

 
     mCurrentLocation = location; 
 
     System.out.println("Current LAT: "+mCurrentLocation.getLatitude()+ " Current LANG: "+mCurrentLocation.getLongitude()); 
 
    } 
 

 
    private void stopLocationUpdates() { 
 
     if (googleApiClient != null && googleApiClient.isConnected()) { 
 
      googleApiClient.disconnect(); 
 
     } 
 
    } 
 
\t 
 
    @Override 
 
    public void onConnected(Bundle bundle) { 
 
     Log.d(TAG, "onConnected"); 
 

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

 
     /*if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
 

 
      return; 
 
     }*/ 
 

 

 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
 
      LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
 

 
     } 
 

 
    } 
 

 
    @Override 
 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
 
     Log.e(TAG, "onConnectionFailed"); 
 

 
     stopLocationUpdates(); 
 
     stopSelf(); 
 
    } 
 

 
    @Override 
 
    public void onConnectionSuspended(int i) { 
 
     Log.e(TAG, "GoogleApiClient connection has been suspend"); 
 
    } 
 
}

ответ

Смежные вопросы