Reputation: 203
In my app, I wanna use both location provider.
That means, if the gps is disabled I want my app to switch to network, and when gps is enabled to switch back it.
I'm using two LocationListener to handle those two requests.
public void onStatusChanged(String provider, int status,Bundle extras)
switch (status) {
case LocationProvider.TEMPORARILY_UNAVAILABLE:
......
break;
case LocationProvider.OUT_OF_SERVICE
.....
break;
case LocationProvider.AVAILABLE
.....
break;
}
And in the each listener ,I detect those status in the onStatusChanged()
.
It turns out, this method will be used in the first change(disabled network),but when I enable the network again, it shows nothing.Why was that? The listener won't detect the status all the time??
Please help me, it would be best to post you solution in code...Thanks!
Upvotes: 5
Views: 5489
Reputation: 1357
This is what I do:
public class LocationActivity extends Activity implements LocationListener{
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private TextView outputField;
private Location location;
private ScrollView scrollView;
private Criteria criteria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
latituteField = (TextView) findViewById(R.id.lat_textView);
longitudeField = (TextView) findViewById(R.id.long_textView);
outputField = (TextView) findViewById(R.id.output_textView);
scrollView = (ScrollView) findViewById(R.id.scrollView1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
List<String> providers = locationManager.getProviders(criteria, true);
outputField.append("Providers available..." + "\n");
for (String provider : providers) {
outputField.append(provider + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if (provider != null) {
outputField.append("Provider " + provider + " has been selected." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
if (location != null) {
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} else {
outputField.append("No provider selected" + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
double lat =location.getLatitude();
double lng =location.getLongitude();
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
outputField.append("New Location: " + String.valueOf(lat) + " " + String.valueOf(lng) + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
@Override
public void onProviderDisabled(String dProvider) {
outputField.append("Provider " + dProvider + " has been disabled." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if (provider != null) {
outputField.append("Provider " + provider + " has been selected." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
if (location != null) {
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} else {
outputField.append("No provider selected" + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
public void onProviderEnabled(String eProvider) {
outputField.append("Provider " + eProvider + " has been enabled." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if (provider != null) {
outputField.append("Provider " + provider + " has been selected." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
if (location != null) {
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} else {
outputField.append("No provider selected" + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
outputField.append("Provider " + provider + " status changed to: " + Integer.toString(status) + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
Upvotes: 4
Reputation: 109237
Your answer is here What is the simplest and most robust way to get the user's current location in Android?. In this Fedor nicely explained with example of how to switching between location provider.
Thanks.
Upvotes: 2