Switching between network and GPS provider

前端 未结 2 1653
庸人自扰
庸人自扰 2020-12-09 12:32

I want to implement a locationListener which will switch between network and GPS providers based on availability.

For example if GPS is not enabled I want it to use

2条回答
  •  旧时难觅i
    2020-12-09 12:45

    Sure, you just get the providers for the network and GPS and pass whichever you want to locationManager.requestLocationUpdates().

    When you want to stop listening to a certain provider, call locationManager.removeUpdates() with the listener object you specified in locationManager.requestLocationUpdates().

    Network:

    Criteria crit = new Criteria();
    crit.setPowerRequirement(Criteria.POWER_LOW);
    crit.setAccuracy(Criteria.ACCURACY_COARSE);
    String provider = locationManager.getBestProvider(crit, false);
    

    GPS:

    Criteria crit2 = new Criteria();
    crit2.setAccuracy(Criteria.ACCURACY_FINE);
    provider2 = locationManager.getBestProvider(crit2, false);
    

    You can use LocationManager.isProviderEnabled() doc to see if the appropriate provider is enabled/disabled. There's more info available in the LocationManager docs.

    GPS is usually much slower than network since you have to find 3+ far-away satellites, etc.

提交回复
热议问题