0

Ich muss den Standort des Benutzers im Hintergrund abrufen, daher versuche ich den Fused Location Provider in meiner Service-Klasse zu verwenden. Ich habe den folgenden Code in die onStart() Methode eingefügt, aber der Kontext MainActivity.getAppContext() wird nicht erkannt. Was kann ich an seiner Stelle verwenden, da ich es in meiner Serviceklasse verwende?Wie kann ich Fused Location Provider in einer Service-Klasse verwenden?

GoogleApi = new GoogleApiClient.Builder(MainActivity.getAppContext()) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 

Antwort

0

A Service ist ein Context so aus dem Service Sie this verwenden können.

Dienst hat auch getApplicationContext(), die den Kontext der Anwendung zurückgibt, was es aussieht MainActivity.getAppContext() sollte wahrscheinlich zurückgegeben werden. Du könntest das auch benutzen.

GoogleApi = new GoogleApiClient.Builder(getApplicationContext()) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
0

Im Folgenden wird erläutert, wie Code in einem Service verschmolzenen Standort-Anbieter verwenden

 import android.Manifest; 
     import android.app.Service; 
     import android.content.Intent; 
     import android.content.pm.PackageManager; 
     import android.location.Location; 
     import android.os.Bundle; 
     import android.os.IBinder; 
     import android.support.annotation.NonNull; 
     import android.support.annotation.Nullable; 
     import android.support.v4.app.ActivityCompat; 
     import android.util.Log; 

     import com.google.android.gms.common.ConnectionResult; 
     import com.google.android.gms.common.api.GoogleApiClient; 
     import com.google.android.gms.location.LocationListener; 
     import com.google.android.gms.location.LocationRequest; 
     import com.google.android.gms.location.LocationServices; 

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

      private String TAG = LocationService.class.getSimpleName(); 
      private GoogleApiClient mGoogleApiClient; 
      private LocationRequest mLocationRequest; 

      private static final long INTERVAL = 1000 * 30 * 60; 
      private static final long FASTEST_INTERVAL = 1000 * 25 * 60; 
      private static final long MEDIUM_INTERVAL = 1000 * 30 * 60; 

      public LocationService() { 
      } 

      @Override 
      public void onCreate() { 
       super.onCreate(); 
       createLocationRequest(); 
        mGoogleApiClient = new GoogleApiClient.Builder(this) 
          .addConnectionCallbacks(this) 
          .addOnConnectionFailedListener(this) 
          .addApi(LocationServices.API) 
          .build(); 
      } 

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

       if (!mGoogleApiClient.isConnected()) { 
        mGoogleApiClient.connect(); 
       } else { 
        startLocationUpdates(); 
       } 
       return START_STICKY; 
      } 


      @Override 
      public IBinder onBind(Intent intent) { 
       // TODO: Return the communication channel to the service. 
       throw new UnsupportedOperationException("Not yet implemented"); 
      } 

      protected void createLocationRequest() { 
       mLocationRequest = new LocationRequest(); 
       mLocationRequest.setInterval(MEDIUM_INTERVAL); 
       mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
       mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
      } 

      @Override 
      public void onConnected(@Nullable Bundle bundle) { 
       Log.v(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); 
       if (mGoogleApiClient.isConnected()) { 
        startLocationUpdates(); 
       } 
      } 

      protected void startLocationUpdates() { 
       if (mGoogleApiClient != null) { 
        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(mGoogleApiClient, mLocationRequest, this); 
        Log.v(TAG, "Location update started ..............: "); 
       } 
      } 

      protected void stopLocationUpdates() { 
       if (mGoogleApiClient != null && mGoogleApiClient.isConnected() && LocationServices.FusedLocationApi != null) { 
        LocationServices.FusedLocationApi.removeLocationUpdates(
          mGoogleApiClient, this); 
        Log.v(TAG, "Location update stopped ......................."); 
        mGoogleApiClient.disconnect(); 
       } 
      } 


      @Override 
      public void onConnectionSuspended(int i) { 

      } 

      @Override 
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

      } 

      @Override 
      public void onLocationChanged(Location location) { 
       Log.v(TAG, "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude()); 
      } 

      @Override 
      public void onDestroy() { 
       super.onDestroy(); 
       stopLocationUpdates(); 
       Log.v(TAG, "Service Stopped!"); 
      } 

     } 
Verwandte Themen