2

I'm developing a Android app and I'm still green in this area.

In my app I'm using the FusedLocationApi to get both the last know location and make Location update requests and I got a problem where, even with both Location and WiFi activated on Android Studio emulator and Samsung Galaxy S5, I never got the LocationListener's onLocationChanged() called.

Here is my code:

package com.trackit.app.locationupdatetest;

import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

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;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private static final String TAG = MapsActivity.class.getSimpleName();
public static final String STARTING_POSITION = "Rio de Janeiro"; //Application's default position
public static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1; //Fine location permission

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private MapsActivity mActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    (findViewById(R.id.Location)).setEnabled(false);
    (findViewById(R.id.StreetView)).setEnabled(false);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    if (mGoogleApiClient == null)
        buildGoogleAPIClient();

    //Create and configure a Location Request object to used while looking for location updates
    if(mLocationRequest == null)
        buildLocationRequest();

    mActivity = this;

}

private synchronized void buildGoogleAPIClient() {

    //Configuring the Google API client before connect
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

private synchronized void buildLocationRequest() {

    mLocationRequest = LocationRequest.create()
            .setInterval(10000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setFastestInterval(1000)
            .setSmallestDisplacement(1);

}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

/**
 * This method is called when the Activity is no longer visible to the user.
 */
@Override
protected void onStop() {
    if(mGoogleApiClient.isConnected())
        mGoogleApiClient.disconnect();
    super.onStop();
}

/**    
 * This callback is triggered when the map is ready to be used.    
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

public void buttonPressed(View buttonPressed){        
    displayUserLocation();      
}

@Override
public void onConnected(@Nullable Bundle bundle) {      

    (findViewById(R.id.Location)).setEnabled(true);
    (findViewById(R.id.StreetView)).setEnabled(true);

}

@Override
public void onConnectionSuspended(int i) {

}

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

    Log.e(TAG,"onConnectionFailed:"+connectionResult.getErrorCode()+","+connectionResult.getErrorMessage());

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull
int[] grantResults) {      

    //Process the user permission's response
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
            processLocationPermissionResult(grantResults);
            break;
        default:
    }        

}

public void processLocationPermissionResult(@NonNull int grantResults[]) {

    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        displayLocation();
    else {} // permission denied, boo! Disable the functionality that depends on this permission.        

}

public void displayUserLocation(){        

    //Verify if the app have permission to access user's location
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.
            ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.
                permission.ACCESS_FINE_LOCATION)) {

            //Asks the permission to access the user's location
            Toast.makeText(this, "This app needs to access your location. " +
                    "Allow the app to access it?", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MapsActivity.MY_PERMISSIONS_REQUEST_FINE_LOCATION);

        }

    }   else displayLocation(); //If the permission was granted

}

public void displayLocation() throws SecurityException{

    Location lastLocation = LocationServices.FusedLocationApi.
            getLastLocation(mGoogleApiClient);  

    if(lastLocation != null) {

        Log.e(TAG, "!!!!!!");

        LatLng position = new LatLng(lastLocation.getLatitude(), lastLocation.
                getLongitude());
        mMap.addMarker(new MarkerOptions().position(position).title("Marker in " +
                MapsActivity.STARTING_POSITION));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(position));

    }   else{

        Log.e(TAG, "??????");
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);

    }        

}

@Override
public void onLocationChanged(Location location) {

    if(location.getAccuracy() < 10 && location.getSpeed() < 55.55555555555556){
        Log.e(TAG, "onLocationChanged() started");          

        LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.addMarker(new MarkerOptions().position(position).title("Marker in " +
                MapsActivity.STARTING_POSITION));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(position));

        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

        Log.e(TAG, "onLocationChanged() terminated");
    }
    /*else{
        //Continue listening for a more accurate location
    }*/

}
}

Below are both my AndroidManifest.xml and my gradle:

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.trackit.app.locationupdatetest">

        <!--
             The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
             Google Maps Android API v2, but you must specify either coarse or fine
             location permissions for the 'MyLocation' functionality. 
        -->
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <!--
                 The API key for Google Maps-based APIs is defined as a string resource.
                 (See the file "res/values/google_maps_api.xml").
                 Note that the API key is linked to the encryption key used to sign the APK.
                 You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
                 You can define the keys for the debug and release targets in src/debug/ and src/release/. 
            -->
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_maps_key" />

            <activity
                android:name=".MapsActivity"
                android:label="@string/title_activity_maps">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.trackit.app.locationupdatetest"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.android.gms:play-services:9.4.0'
}

I'v searched a lot this week but I've got no helpful answer yet =s

These are some of the threads I've searched (I don't want to look like a lazy guy, because I'm not, I'm just stuck)

onLocationChanged does not get called using fusedLocationAPI.requestLocationUpdates

Unable to get location updates

onLocationChanged not called on some devices

on locaton changed never gets called in android google client api

onLocationChanged isn't being called

Can someone point my errors? =S

Community
  • 1
  • 1

2 Answers2

1

I'll venture this answer... if it's helpful, great... if not, sorry...

I have a similar Activity that uses the FusedLocationprovider.

I'm no expert, and can't say WHY yours is not working, but mine is working, and I've noticed the following differences:

YOUR CODE:

private synchronized void buildGoogleAPIClient() {

    //Configuring the Google API client before connect
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

private synchronized void buildLocationRequest() {

    mLocationRequest = LocationRequest.create()
            .setInterval(10000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setFastestInterval(1000)
            .setSmallestDisplacement(1);

}

MY CODE:

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

protected void createLocationRequest() {
    this.mLocationRequest = new LocationRequest();
    this.mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    this.mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    this.mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

Differences:

1) You put:

if(mLocationRequest == null)
        buildLocationRequest();

in your onCreate method. Is this necessary? Wouldn't mLocationRequest always be null in onCreate()?

2) Your methods are private, mine protected

3) your createLocationrequest() is labeled as 'synchronized' ... could is be not working because of this?

4) you use the setSmallestDisplacement() method. I have seen posts that say this method doesn't work properly and results in onLocationChanged() not being called.

5) You instantiate your LocationRequest with: 'LocationRequest.create()' and not "new LocationRequest()' could this be the problem?

Does this help?

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
1

Here is an alternate answer.

There is one line of code that is vital in order for onlocationChanged to ever be called:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);

And in your case, this code resides very deeply in your code with many conditions needing to be met for this to be reached:

Conditions you require:

  • security exception not thrown
  • last location == null
  • (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  • !(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  • button pushed

I would consider putting MORE LOGS in your code in order to determine that these conditions are in fact being met... trace backwards from:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);

PS - I see you're using the Android N permission model. I have yet to put my toes in that water due to the confusion it seems to create... I just set my target SDK = 22. :)

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100