LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is not reliable, why?

前端 未结 4 1287
孤城傲影
孤城傲影 2020-12-06 02:55

One user of my app reported that app tells network for location is off even he did turn it on. He sent me few screen shots and they made me think;

LocationMa         


        
相关标签:
4条回答
  • 2020-12-06 02:57

    Developer support provided by Sharp corporation is excellent and they answered to my question in less than 48 hours.

    This is what I got from them.

    There are 2 conditions must be met that LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) returns true.

    1. Some internal state is ready for network location.
    2. Network location is enabled on setting screen.

    Second one is obvious. But first one is not. They told how to simulate first one is negative. You can confirm the issue with steps shown below and running my test app (please see my question for link to download).

    • Open settings of you phone.
    • Tap Applications.
    • Tap All tab.
    • Find "Network Location", tap it.
    • Tap "Disable".
    • Reboot your phone.
    • Run test app.

    For reason I can't understand the user's phone failed to do something related to first condition shown above and exhibits the issue.

    Conclusion:

    LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is reliable. And be aware, Secure.LOCATION_PROVIDERS_ALLOWED is less reliable.

    0 讨论(0)
  • 2020-12-06 03:04

    Location Manager is not reliable on some phones. You may notice that if you launch google maps all of a sudden your app works. That is because Google Maps kicked the LocationManager. Which also means that there is programmatic way to kick that dude alive. So I used

    HomeScreen.getLocationManager().requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
            @Override
            public void onLocationChanged(final Location location) {
            }
        });
    

    After the above code, I called what ever I needed from LocationManager and it kinda worked. If not try out the new API's LocationClient. This is suppose to be much better, battery, accuracy and reliability.

    0 讨论(0)
  • 2020-12-06 03:08

    Not directly an answer to your question(s), but check out the new Location API that Google launched last week. It's really easy to implement and it will check for the best possible location without wasting battery. http://developer.android.com/google/play-services/location.html and here's a session at Google I/O about this new API and how to use it. http://www.youtube.com/watch?v=Bte_GHuxUGc

    This way you don't need to worry about checking if the GPS is on or not and stuff like that

    0 讨论(0)
  • 2020-12-06 03:15

    The modern way to check the users Location settings is through LOCATION_MODE in Settings.Secure

    For example if you simply want to know if the user has disabled them or not, you can do:

       public static boolean isLocationEnabled(Context context) {
           return getLocationMode(context) != Settings.Secure.LOCATION_MODE_OFF;
       }
    
       private static int getLocationMode(Context context) {
           return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
       }
    

    This will return true if Location is enabled. If you need finer granularity see the docs for details.

    This method is better suited when using the Google Services Location APIs than the old NETWORK_PROVIDER and GPS_PROVIDER ways. Note: Requires KitKat / API19

    0 讨论(0)
提交回复
热议问题