Question

I have done this code where I ask for updates on location.I want to know if i implemented it correctly. Once page is opened, i request for updates ( preference to network over gps)...and i display the location in function onlocationchanged. I want to ask is it possible inside onlocationchanged i get location null similar when i do getlastknownlocation? i stopped using ghetlastknownlocation as it returns nulll at times and wrong values.

      boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean  isNetworkEnabled = lm
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!isGPSEnabled && !isNetworkEnabled)
    {   
        ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
        progress.setVisibility(View.INVISIBLE);
        Utilities.showAlertDialog2(RegionalStoresMeActivity.this, "", ""+getResources().getString(R.string.nothing_enabled), false);
    }
    else if(isNetworkEnabled){
        // lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);       
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    }
    else if(isGPSEnabled ) {                                                                                      
               lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0, this);
    } 
Was it helpful?

Solution

The onLocationChanged() method is only called when their is a location update with the appropriate Latitude and Longitude. It might be null to slightest possibility but I've never experienced it.

U've done it properly but I'll like to make a suggestion. Consider a scenario-

The network provider is enabled and u've requested location updates. But for some reason it does not returns location update (there r many cases in which Location provider dosen't return location update also it is not compulsory to return the location updates. )

Now as per ur code it won't check the GPS provider cos its in "else if( isGPSEnabled )". So I'll suggest u to implement it in only "if" statement like below

if( isNetworkEnabled )
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);

if( isGPSEnabled )                                                                                      
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0, this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top