Android GPS does not work until restart on new phones

别来无恙 提交于 2019-12-03 08:05:03

Looks like you're registering both the GPS and NETWORK providers to listen for a location for 10 seconds, and when the timer goes off after 10 seconds you try to get the most recent location from both providers.

There are a few things going on here.

First, you seem to be listening for updates in the wrong method. Your two listeners should look like:

    LocationListener locationListenerGps = new LocationListener() {

            // This will never be called, its not part of the LocationListener interface - http://developer.android.com/reference/android/location/LocationListener.html
            /* public void onStatusChanged(Location location) {
                    timer1.cancel();

                    locationResult.gotLocation(location);
                    lm.removeUpdates(this);
                    lm.removeUpdates(locationListenerNetwork);
            } */
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
            public void onLocationChanged(Location location) {
                    // This is the correct method to receive location callbacks
                    timer1.cancel();

                    locationResult.gotLocation(location);
                    lm.removeUpdates(this);
                    lm.removeUpdates(locationListenerNetwork);

            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

Second, I'd use the ScheduledThreadPoolExecutor or Handler instead of Timer.

From the Timer docs:

Prefer ScheduledThreadPoolExecutor for new code...This class does not offer guarantees about the real-time nature of task scheduling.

If a reboot is required to get the app working, its likely something to do with the Timer not firing after 10 seconds. Note that this doesn't necessarily mean that GPS itself isn't working.

Handler should do the job and it's designed for Android, so I'd suggest using it. It looks like:

Handler handler = new Handler();
handler.postDelayed(getLastLocation, 10000);

...and your GetLastLocation would change to:

Runnable getLastLocation = new Runnable() {
    @Override
    public void run() {
        ...
    }
}

...and your cancel() and other methods would need to reference the Handler.

Also, note that you're declaring the Location object in your MainActivity with a provider type of NETWORK_PROVIDER, and then setting the lat/long in that object.

public Location mUserCoordinates = new Location(LocationManager.NETWORK_PROVIDER);

So, the location type in MainActivity will always appear to be of NETWORK_PROVIDER, no matter the actual source.

Also, doesn't look like your MainActivity needs to implement LocationListener, as its never registered with the LocationManager.

Finally, instead of using two listeners for GPS and NETWORK, I would suggest using the Fused location provider in Google Play Services, as discussed here: http://developer.android.com/training/location/receive-location-updates.html

You'll be limited to devices Android 2.2 and up with Google Play Services installed, but in my opinion its worth it to avoid dealing with some of the eccentricities of location in the platform and managing more than one provider. For more about Fused location provider and how it differs from listening directly to GPS and NETWORK providers, see this 2013 Google I/O presentation - Beyond the Blue Dot: New Features in Android Location

Why do you use Google Play Services Location API?

The only new feature provided by Play Services is Geofencing. From your answer i assume that you don't want to use Geofencing but just "usual" location requests.

The Android platform provides a great API for such requests which does not requires Google Play Services. I never had the problem you described when using it.

Note that although Google claims the Play Services to be better than the Android API, this is not true since API 9 (Android 2.3), as long as you use the newer LocationManager.requestLocationUpdates methodes that don't require a provider to be specified.

See: http://developer.android.com/guide/topics/location/strategies.html

I had the same issue with the regular Location API earlier. I swiched to Play Services, and it seemed to work. Lately sometimes I experience this issue again with Google Play Services. It is really strange, and based on my experience the probelem is system-wide, so when my app couldn't find location, than the Google Maps app couldn't eather. Note that I use Cyanogenmod, so it can be some bug within it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!