Frage

I try to get location of user on Android device. Program checks the GPS usability. If GPSProvider is disable it warns the user. If user doesn't enable GPS, program check for networkProvider.

code for checking network provider,

public boolean askNetworkProvider(){

    if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        AlertDialog builder = new AlertDialog.Builder(context)
                .setMessage("Enable Network Connection")
                .setTitle("Warning!")
                .setCancelable(true)
                .setPositiveButton("OK", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                } ).show();
    }

    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, networkListener);
         mlocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
         Log.i(LocationService.class.getName(),"Network Enable!!");
         return true;

     }
    return false;
}

Even though wifi and mobile connection is disable, it says networkProvider is enable and enter second if statement. But it should be false. How can i fix it?

War es hilfreich?

Lösung

Probably you were disabling the main menu's Wi-Fi connection on the drop down options instead of disabling the "Use wireless networks" under Settings-Location Services.

Bear in mind that Wi-Fi != Use Wireless Networks.

When you use

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

it is cheking the "User Wireless Networks" option.

I had the same problem, many alike questions were posted at StackOverflow and only one answer had this information. Took me a whole afternoon. Hope you got it! :)

Andere Tipps

In new Android 4.3 (API level 18 and above) and devices using that, there's an advanced WiFi setting: "Scan always available". If its checkbox is checked, the device is able to scan for WLAN & location even if WiFi is switched off, and therefore will most likely enter the second if statement. To get this fixed programmatically, you could add build version check (and probably some other settings checks) to the if statement. (And you should define how you would want it to work in that case.) The easy manual workaround is of course to disable the setting: e.g. in Nexus 7: Settings->WiFi->Advanced-> uncheck "Scanning always available".

I think for you it is better to use googlePlay sdk for the GPS.

It is working like a charm with out stupid bugs. I changed your code to the googlePlay sdk and it is perfect for my users. (No complains)

I don't think "enabled" means what you think. All you're checking for is to see if the user has allowed the phone to use the location provider, i.e. enabled location services to get location via wifi. This does not mean that the NetworkProvider is or will be successful in returning a result, just that the user has allowed it to be accessed.

The difficulty is that using the LocationManager there is no "updated failed" response. You will only be notified if the user's location changes within your parameters. If it never does, there's not much you can do about it.

I found it easier to get location using GooglePlayServices and then switch to the standard location manager as a back up if play services was either not available or returned an error. You can see how to do this here. There's also a great post on the android developers blog which demonstrates how to combine the two approaches to use google play if available and switch to the regular location manager if unavailable

Google updated its location logic with fused location implementation. I guess you'll find this sample interesting. While getting location updates you don't have to define which provider is enabled or not. http://www.kpbird.com/2013/06/fused-location-provider-example.html?m=1
Edit: This is not the answer of your question but in my opinion fused location implementation is the best practice for location listening. I hope you implement your location listening structure on fused location.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top