Android failing to get location after a phone reboot
I'm having a problem getting the user's location on one of my tester's phones. It always works fine for me so I don't know what the difference is.
Here is the sequence:
1: Reboots his phone. 2. Checks to make sure location services are enabled (for both GPS and Network). 3. Starts my app.
The OS never provides a valid location. The getLastKnownLocation() always returns null for both GPS_PROVIDER and NETWORK_PROVIDER and the LocationListener is never called after requestingLocatioUpdates.
- He then runs the Google Maps app (which does seem to locate him).
- Then if he runs my app again and it does get the location correctly.
Here are the basics of 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 !=开发者_JAVA百科 null )
useLocation( lastLocation );
else
useLocation( locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER ) );
When it fails, the code says the best provider is "network" so that is what I use. But the onLocationChanged is never called. I also never get a non-null value from the getLastKnowLocation().
Is there some further magic I need to do to get this to work consistently after a reboot?
I presume that you have written this in a service.If thats the case try using a BroadCastReciever that listens for Boot events.For example
<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>
Then in the BootReceiver file call your service.
Better solution is not trying get the best provider once (you cannot be sure it will be GPS or Network), but listen to both GPS and Network providers and decide which location to use judging by their accuracy and provider:
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);
In 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
}
}
try changing this code
locationManager.requestLocationUpdates( provider, 120 * 1000, 10000, locationListener );
to
locationManager.requestLocationUpdates( provider, 120 * 1000, 0, locationListener );
it may be because your are withing your 1000 meter
I had the same issue with Samsung Galaxy 3 and Nexus device using GPS_PROVIDER, once I started using both GPS & Ntwork.. It picks up the location whichever being returned first and shows to the users. One thing, I have the distance specified is 300 meters and time interval is 10 secs
I had exactly this same problem myself. I'd forgotten to add the permissions to access the ACCESS_COARSE_LOCATION
and ACCESS_FINE_LOCATION
location/permissions in my manifest.
After adding those and rebuilding, it started working as expected.
精彩评论