Android GPS does not work until restart on new phones

前端 未结 3 2048
自闭症患者
自闭症患者 2021-02-09 13:42

I have posted about this issue before, and found a few other people who have had the same issue with no solutions found.

I am developing an Android app that submits a JS

3条回答
  •  既然无缘
    2021-02-09 14:21

    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

提交回复
热议问题