Android will not be able to find a place after rebooting the phone

I have a problem with the user's position on one of my tester phones. This always works well for me, so I don’t know what the difference is.

Here is the sequence:

1: reboots your phone. 2. Checks to make sure location services are turned on (for both GPS and network). 3. Launches my application.

The OS never provides the correct location. GetLastKnownLocation () always returns null for both GPS_PROVIDER and NETWORK_PROVIDER, and the LocationListener is never called after the LocatioUpdates request.

  • Then it launches the Google Maps application (which seems to find it).
  • Then, if it starts my application again and it sets the location correctly.

Here is the basic information about my code:

    LocationManager locationManager = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
    LocationListener locationListener = new LocationListener()
    {

        public void onLocationChanged( Location location )
        {
            useLocation( location );
        }

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

        public void onProviderEnabled( String provider )
        {
        }

        public void onProviderDisabled( String provider )
        {
        }
    };

    // Register the listener with the Location Manager to receive location updates
    // Checks a minimum of every 120 seconds.  Location must change by 10000 meters 
    String provider = locationManager.getBestProvider( new Criteria(), true );
    if ( provider != null )
    {
        System.out.println( "Using provider: " + provider );
        locationManager.requestLocationUpdates( provider, 120 * 1000, 10000, locationListener );
    }

    // Get the last know location for immediate use
    Location lastLocation = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
    if ( lastLocation != null )
        useLocation( lastLocation );
    else
        useLocation( locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER ) );

, , - "", . onLocationChanged . getLastKnowLocation().

- , , ?

+3
5

, . , BroadCastReciever, .

<receiver android:name="com.singulera.familysiren.receiver.BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
        </action>
        <category android:name="android.intent.category.HOME">
        </category>
    </intent-filter>
</receiver>

BootReceiver .

+1

( , GPS ), GPS , , :

locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, PERIOD_UPDATE_GPS*1000, DISTANCE_UPDATE_GPS, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, PERIOD_UPDATE_NETWORK*1000, DISTANCE_UPDATE_NETWORK, this);

onLocationChange:

@Override
public void onLocationChanged(Location locationNew) {
    // for example, check if location was provided by GPS
    if (locationNew.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                // do your work here
    }
}
+1

locationManager.requestLocationUpdates( provider, 120 * 1000, 10000, locationListener );

 locationManager.requestLocationUpdates( provider, 120 * 1000, 0, locationListener ); 

, 1000-

0

Samsung Galaxy 3 Nexus, GPS_PROVIDER, GPS Ntwork. , , . , - 300 , - 10

0

I had the same problem. I forgot to add the permission to access the location / resolution ACCESS_COARSE_LOCATIONand ACCESS_FINE_LOCATIONin my manifesto.

After adding and rebuilding, it began to work as expected.

0
source

All Articles