Problem with reciving information from GPS on Android
I just started playing with android device and have problem with getting updates from GPS listener.
Firstly, the code, so You could check if I'm doing everything right:
In AndroidManifest.xml I have:
<uses-permission android:name="android.permission.LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
Location listener is defined as private member of Activity:
private LocationListener mLocationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location pLocation) {
double lLatitude = pLocation.getLatitude();
double lLongitude = pLocation.getLongitude();
mGpsTextView.setText ("Longitude" + Double.toString(lLongitude) + " Latitude: " + Double.toString(lLatitude));
}
@Override
public void onProviderDisabled(String provider) {
mGpsTextView.setText ("Provider disabled");
}
@Override
public void onProviderEnabled(String provider) {
mGpsTextView.setText ("Provider enabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status)
{
case LocationProvider.OUT_OF_SERVICE:
mGpsTextView.setText("GPS out of service");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
mGpsTextView.setText("GPS temorarily unawalible");
开发者_如何学Go break;
case LocationProvider.AVAILABLE:
mGpsTextView.setText("GPS avalible");
break;
default:
mGpsTextView.setText("GPS crazy");
}
}
};
In "onResume" method I aquire LocationManager and register my listener:
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0.0f, mLocationListener);
In "onPause" I remove listener
mLocationManager.removeUpdates(mLocationListener);
Then, when I test the code on emulator with geo fix - everything works perfect. But when I deploy my app on phone the code not work as it should. When the provider is disabled the text on the "mGpsTextView" is "provider disabled" - so it is ok. But when I run app with gps provider enabled the text on "mGpsTextView" and no updates are recived (checked it with debugger - onLocationChanges isn't called).
Any idea what may be wrong?
Why are you using Sensor.TYPE_ACCELEROMETER?
It should be something like this:
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 100.0f, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0.0f, locationListener);
精彩评论