delight-im / Android-SimpleLocation

Utility class for easy access to the device location on Android
Apache License 2.0
200 stars 75 forks source link

Received first coordinates (not 0, 0), but values did not update afterward. #40

Closed khaleelasyraaf closed 4 years ago

khaleelasyraaf commented 4 years ago

Hi there!

First of all, I would like to thank you for this awesome library. It does help me with my project, also I'm relatively new to Android programming. However, I'm having a small issue that maybe you could help me with. I've followed the instructions on the README file. Everything is running smoothly but then I realized that the first coordinates I received did not update afterward. It keeps giving the same exact coordinates every 10 seconds even when I go out for running.

Note: I've used a Runnable for this to save the coordinate values every 10 seconds into .csv file until I pressed a button to stop it.

In MainActivity file:

private SimpleLocation mLocation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("onCreate", "onCreate");

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
    }

    mLocation = new SimpleLocation(this);

    if (!mLocation.hasLocationEnabled()) {
        // ask the user to enable location access
        SimpleLocation.openSettings(this);
    }

}

private Runnable mGPSRunnable = new Runnable() {
    @Override
    public void run() {
        final double latitude = mLocation.getLatitude();
        final double longitude = mLocation.getLongitude();            
        Log.d("GPS", "Lat: " + latitude + " Long: " + longitude);
        mHandler.postDelayed(this, 10000);
        saveGPSData(); // saves the values in .csv file
    }
};

The two methods below are assigned to one button each:

public void startGPS() {
    mGPSRunnable.run();
    mLocation.beginUpdates();
}

public void stopGPS() {
    mHandler.removeCallbacks(mGPSRunnable);
    mLocation.endUpdates();
}

Did I forget something here?

My apologies for the long post. I would really appreciate if you can help me with this :)

ocram commented 4 years ago

You have to configure the instance via the constructor: https://github.com/delight-im/Android-SimpleLocation#extended-constructor-options

So you probably want requireFine to be set to true, interval set to something less than 10 minutes (in milliseconds), and requireNewLocation set to true.

By the way, perhaps you might also want to return after your call to ActivityCompat.requestPermissions.

Does that help?

khaleelasyraaf commented 4 years ago

Sorry for the late reply as I was swarmed with other things recently.

Now it returns 0,0. I've checked all the previous 0,0 issues, applied the changes, and still I get 0,0. Check below.

    mLocation = new SimpleLocation(this, true, false, 5000, true);

    mLocation.setListener(new SimpleLocation.Listener() {
        public void onPositionChanged() {
            Log.d("Location", "There are changes");
            // new location data has been received and can be accessed
        }
    });

When you mentioned that the GPS will take some time to update its first location, how long would that be? I received no updates on the first location while waiting for nearly 10 minutes.

khaleelasyraaf commented 4 years ago

I did receive the Log.d("Location", "There are changes"); but only when I set requireFine to false. Otherwise, the location did not update and stays 0,0.

ocram commented 4 years ago

Do you think it would be possible that no (new) GPS signal has been available? Has this been tested inside or outside?

And is this something you’re running in the foreground (e.g. Activity) or in the background (e.g. background service)? It seems it’s the former right? That shouldn’t be a problem then.

Had the permission been granted for fine/GPS?

Finally, when not requiring fine/GPS, did it update (“There are changes”) just once, or multiple times? What was the duration between updates? Indeed about 5 seconds? Did the location data actually change?

khaleelasyraaf commented 4 years ago

Do you think it would be possible that no (new) GPS signal has been available? Has this been tested inside or outside?

I've tested this outside, had a 30mins walk today, but still the location is not updated. Could it be because of the device? I'm using Sony Xperia Z3.

And is this something you’re running in the foreground (e.g. Activity) or in the background (e.g. background service)? It seems it’s the former right? That shouldn’t be a problem then.

This shouldn't be the problem as I've implemented the background service to keep the app running all the time even when the phone is locked.

Had the permission been granted for fine/GPS?

It has been granted when I ran the app for the first time. I've included android.permission.ACCESS_FINE_LOCATION and android.permission.ACCESS_COARSE_LOCATION in the manifest file too.

Finally, when not requiring fine/GPS, did it update (“There are changes”) just once, or multiple times? What was the duration between updates? Indeed about 5 seconds? Did the location data actually change?

It updates (“There are changes”) multiple times every 5 seconds. The first value is always 0,0. When it updates, the location data changed, but only once. The next incoming coordinate values stay the same as the previous values even when (“There are changes”) happens. Check the image.

gps_error_1

I hope these are not too confusing.

ocram commented 4 years ago

Testing outside and trying for 30 min sounds good. No problem there.

Declaring both permissions in the manifest sounds good as well.

If your device is running Android 6 or later, can you check in the app details in the device settings whether both have actually been granted?

The update interval of the coarse data looks good. So were you outside when testing this as well, and did you move?

And are you sure background processing is not a problem? Is your device running Android 8 or later or Android 10 perhaps? If so, keep the following in mind:

If your app targets Android 10 (API level 29) or higher, also check for the ACCESS_BACKGROUND_LOCATION permission. [1]

Look for use of location access APIs […] within your code such as in the following constructs [1]:

  • Background services
  • JobIntentService objects
  • WorkManager or JobScheduler tasks
  • AlarmManager operations
  • Pending intents that are invoked from an app widget

If background location access is essential for your app, keep in mind that Android preserves device battery life by setting background location limits on devices that run Android 8.0 (API level 26) and higher. On these versions of Android, if your app is running in the background, it can receive location updates only a few times each hour. [1]

When a foreground service is running, the system raises user awareness by showing a persistent notification. Your app retains access when it's placed in the background, such as when the user presses the Home button on their device or turns their device's display off.

Additionally, it's recommended that you declare a foreground service type of location, as shown in the following code snippet. On Android 10 (API level 29), you must declare this foreground service type. [2]

In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently background apps can retrieve the user's current location. Apps can receive location updates only a few times each hour.

Note: These limitations apply to all apps used on devices running Android 8.0 (API level 26) or higher, regardless of an app's target SDK version. [3]

khaleelasyraaf commented 4 years ago

If your device is running Android 6 or later, can you check in the app details in the device settings whether both have actually been granted?

My device is running Android 6. I've checked the app's permission in the device settings and it shows that the location has been enabled. Or is there another way to check it?

The update interval of the coarse data looks good. So were you outside when testing this as well, and did you move?

Yes exactly. I started the GPS, locked the phone, and went for a walk.

And are you sure background processing is not a problem? Is your device running Android 8 or later or Android 10 perhaps? If so, keep the following in mind:

Yes I do realize this. Apart from GPS, I'm also collecting the accelerometer and gyroscope sensor values. The app is designed to run in the background, thus I've implemented the AlarmManager and a foreground service to avoid the system from killing the app even when the phone is locked. As a result, the app is capable of running for at least 4 hours (I've tested it), as long as it listens to the sensors. It works for the two sensors, which I received different readings every 10 seconds, but not the GPS. Since my device is Android 6, I don't think the limitation applies. Even if it does, the coordinate values should update a few times at least. The picture I sent you, it has the same values for 35 mins. I've also included ACCESS_BACKGROUND_LOCATION prior to that.

I haven't tried out leaving the app in the foreground while walking. Perhaps I should do that? Maybe I should try longer than 35mins? Or it could be the device is old or not supported? Is there such a case?

I'm sorry again about having this issue.

ocram commented 4 years ago

My device is running Android 6. I've checked the app's permission in the device settings and it shows that the location has been enabled. Or is there another way to check it?

No, that’s correct. If you go to “Settings” → ”Apps” → Your app → ”Permissions” or similar and check if the runtime permissions have been granted, that’s all you need to do.

By the way, you should check the “Location” settings in the device settings as well. It’s possible that the fine/GPS provider is disabled there.

Since you’re using a Sony, make sure to also disable the “Stamina” battery saver in “Settings” → “Battery” or similar.

Since my device is Android 6, I don't think the limitation applies.

Yes, correct.

I haven't tried out leaving the app in the foreground while walking. Perhaps I should do that?

In order to simplify debugging and help track down the problem, yes, I would do this.

Maybe I should try longer than 35mins?

I would try only five minutes. If after five minutes of being outside, you haven’t received any GPS location (or still exactly the same coarse location), something is wrong.

Or it could be the device is old or not supported? Is there such a case?

That’s unlikely. Though you could certainly use Google Maps, this, this or this to test if other apps get the location.

khaleelasyraaf commented 4 years ago

It seems to be working now! I received different values after a few minutes.

These are the things I did:

  1. I haven't used Google Maps on this device for a long time, so when I opened it, the map location on the screen was updated.
  2. I changed compile into implementation when I declare the Gradle dependency in my app module's build.gradle since I received a warning that says compile is deprecated. (Not sure if this was the issue, but just a remark from my side).
  3. After I did 1 and 2, I set the requireFineGranularity to true, then I received different readings when (“There are changes”).

I will test it out later or tomorrow when I go running, but since the values are changing slightly because I'm not outside and the device is idle, it should be working properly when I leave the house. I will keep you posted! :)

ocram commented 4 years ago

Thanks. That’s strange, since neither 1 nor 2 should have any effect on this. But it’s great to hear that it’s working now. Looking forward to hearing about your experience with this when you’re outside again.

khaleelasyraaf commented 4 years ago

Good news! Came back from running and plotted the coordinates, works like a charm. It updates the values every 10 seconds and they're very precise. I'm not sure what was the main issue prior to this. Regardless, I thank you for this awesome library and also your time for helping me out. Have a nice week ahead!

ocram commented 4 years ago

That’s great to hear – although we don’t know the exact reason.

Anyway, good luck with your project, and have a great week, too!