5

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!

alvan
  • 203
  • 4
  • 11
  • Guys, the new solution is an combination of user370305 and Fedor's work. Fedor did a great to job to initialize and choose the right location provider. And with the help of user370305 . We can start a thread to detect the network/gps status. Because, if you do a `removeUpdates()`, the listener will no longer work. So, the `case LocationProvider.AVAILABLE` is useless. You can only use the thread to make a new request.(I know it is not a very good one,so if you have a better solution, please let me know, I will be love to see that!!Thanks!) – alvan Nov 09 '11 at 07:35

2 Answers2

4

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);
    }
}
Islam A. Hassan
  • 1,357
  • 15
  • 20
2

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.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I think Fedor's work just identified which provider could be used at the initialization phase. Are you saying I should new a thread to detect the `isProviderEnabled`? – alvan Nov 08 '11 at 09:45
  • ok, [How to switch provider into GPS_provider if android phone doesn't have internet connection?](http://stackoverflow.com/questions/7776049/how-to-switch-provider-into-gps-provider-if-android-phone-doesnt-have-internet/7776108#7776108) in this look at my answer. and let me know if its helpful for you or not. – user370305 Nov 08 '11 at 09:54
  • Thanks, your code give the hint that I should start a thread to check CONNECTIVITY_SERVICE. – alvan Nov 09 '11 at 07:29