• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Using LocationServices

Hi,

I am trying to follow the chapter 33 of Programming Android book (edition 3). I cannot get the following method work. First, I am getting a compiler error with red squiggly line "Call requires permission which may be rejected by user..." But I've got the permission implementation done.

When I introduce the following if statement (by selecting from the list when Alt+Enter) the above red squiggly error goes away. But the onLocationChanged(Location location) method is never executed.

This line Log.i("findImage", "onLocationChanged"); never executes.

Java:
private void findImage(){
    Log.i("findImage", "findImage - first line");

    LocationRequest request = LocationRequest.create();
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    request.setNumUpdates(1);
    request.setInterval(0);

    Log.i("findImage", "findImage - after request");

    if(hasLocationPermission()){
        Log.i("findImage", "findImage - if(hasLocationPermission())");

        LocationServices.FusedLocationApi
                .requestLocationUpdates(mClient, request, new LocationListener() {

                })

        try {
            LocationServices.FusedLocationApi
                    .requestLocationUpdates(mClient, request, new LocationListener(){
                        @Override
                        public void onLocationChanged(Location location){
                            Log.i("findImage", "onLocationChanged");
                        }
                    });
        } catch (Exception ex){
            Log.i("findImage", "findImage - GeneralException" + ex.getMessage());
        } finally {
            Log.i("findImage", "findImage - Finally block");
        }
    }

    Log.i("findImage", "findImage - last line");
}

Any suggestions please?
 
That's not an error. It's just warning you that the permission must be confirmed by the user, so your code should not rely on having that permission confirmed.
 
I see, thanks for your reply. That explains then why I am not getting any exception messages there. At least I know this is not related to the other issue. The other issue is: According to the book the log line inside of the "onLocationChanged" method should be executed, but it never does. I am getting the following outputs in the logcat:

findImage - after request
Finally stm
findImage - last line

But it also should output this from inside this method:
findImage - onLocationChanged,

I would appreciate for any thought on this please.
 
Your code isn't being executed because this line

Code:
if(hasLocationPermission()){

evaluates to FALSE. Therefore execution will skip over that block. You need to use the correct procedure for requesting permissions from the user.

But let me also advise you that debugging your code using Log statements is a most inefficient way of development. If you want to know what parts of your code are being executed, then run the app in debug mode, and set breakpoints at specific lines. You can then step through and see what happens. You can also examine the run state of your app.
It's all explained here https://androidforums.com/threads/please-read-me-before-posting.987318/
 
Thanks, I will start using the debugger - I am new to the Android Studio so still getting my head around.

Sorry, my code above is a bit outdated now, I've pasted the amended version below - I've removed the if statement and that is why is not printing out the if... I have changed the hasLocationPermission method by returning hard-coded 'true'. This still didn't help.

Java:
private void findImage() {

    LocationRequest request = LocationRequest.create();
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    request.setNumUpdates(1);
    request.setInterval(0);

    Log.i("findImage", "findImage - after request");

    try {
        LocationServices.FusedLocationApi
                .requestLocationUpdates(mClient, request, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        Log.i("findImage", "onLocationChanged");
                    }
                });
    } catch (Exception ex) {
        Log.i("findImageCatch", "Exception ex");
    } finally {
        Log.i("findImageFinally", "Finally stm");
    }

    Log.i("findImage", "findImage - last line");
}
 
Thanks for your answers. I will try the link. I just followed the step-by-step exercises from the Programming Android book and it should have worked as is.
 
I agree, but unfortunately I am stack on this book because I am trying to do my coursework, which is based on this book exercises. This is the book I am using: https://www.bignerdranch.com/books/android-programming/

To be honest I am really annoyed because I've spend few days on this particular issue and still can't make it work. Because of this I haven't even started my coursework, which I've only got few days left to submit. Really annoyed.
 
In the end I found the solution to this particular problem. The android phone I was given for testing and debugging purposes didn't have google api services installed or updated versions on it. So I've installed/updated all the google services and now the 'onLocationChanged' callback executes and prints the location details. What a waist of 3 days.

Thanks for the support I was given here - really appreciated.
 
Back
Top Bottom