1

Sorry for the long title but couldn't resume it more, basicly i implemented the googlefusedlocation api as a service, i need the service to run on background and update the location, this service starts on my app running (atm i am not requesting the user if he want to share location because i have no idea how ti implement it with a service).

My service:

package com.example.afcosta.inesctec.pt.android.services;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Binder;
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.support.v4.content.ContextCompat;
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 GoogleLocation extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private static final String TAG = "BOOMBOOMTESTGPS";
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 0;
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 0;
    private static final float LOCATION_DISTANCE = 0f;
    private int updatePriority;
    private GoogleApiClient googleApiClient;
    private LocationRequest locationRequest;

    private final IBinder mBinder = new LocalBinder();
    private Intent intent;
    private String provider;
    Context context;


    Location mLastLocation;

    public class LocalBinder extends Binder {
        public GoogleLocation getServerInstance() {
            return GoogleLocation.this;
        }
    }

    public Location getLocation() {
        Log.d("IMHERE", "HELLO");
        return mLastLocation;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        context = getApplicationContext();
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }


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


    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");
        initializeLocationManager();

        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            this.updatePriority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            this.updatePriority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
        } else {
            this.updatePriority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        }

        this.buildGoogleApiClient();
        this.createLocationRequest();
        this.googleApiClient.connect();

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(this.googleApiClient, this.locationRequest,this);

        } else {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

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

    }

    @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;
    }


    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();

        this.googleApiClient.unregisterConnectionCallbacks(this);
        this.googleApiClient.unregisterConnectionFailedListener(this);
        this.googleApiClient.disconnect();
        this.mLastLocation = null;
    }



    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }

    private synchronized void buildGoogleApiClient() {
        this.googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    private void createLocationRequest() {
        this.locationRequest = new LocationRequest();
        this.locationRequest.setInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        this.locationRequest.setPriority(updatePriority);
    }

then on my camera i access the getLocation method that is inside my service, it gives me the last location, i just display the way i acces the service if you guys ask, but i just remember that i had a solution before(i used locationListener and not google one) and i could get the location, failled sometimes thats why i tried to change.

 public void onServiceConnected(ComponentName name, IBinder service) {
        mBounded = true;
        GoogleLocation.LocalBinder mLocalBinder = (GoogleLocation.LocalBinder)service;
        mlocation = mLocalBinder.getServerInstance();
        location = mlocation.getLocation();
        Log.d("localizacao",location.toString());
    }

Now i get:

lat:null lon:0.0 alt:0.0

Strange, why is that happening?

Thanks

afcosta007
  • 93
  • 7
  • Can you show the relevant code in your Activity? – Daniel Nugent Jun 30 '17 at 17:42
  • updated, i could get location out of building, but is soo inacurated – afcosta007 Jun 30 '17 at 17:45
  • Looks like you're trying to get a Location immediately on app startup. You will need to wait until the `onLocationChanged()` method override in the Service is called. You can use an interface callback in order to send the Location from the Service to the Activity once the Service gets a Location. See here: https://stackoverflow.com/a/23587641/4409409 – Daniel Nugent Jun 30 '17 at 18:10

0 Answers0