Search code examples
androidbroadcastreceiverlocationmanagerupdating

Android: locationManager requestSingleUpdate working on one device but not on the other


First of all: This is my first question on Stackoverflow so please tell me if i make any mistakes :)

I want my Activity to get the users location everytime it is started. I don't need periodic updates so I try to use requestSingleUpdate. After following this tutorial: http://androidexperinz.wordpress.com/2012/04/19/current-location-update/

It worked perfectly on Samsung Galaxy S2 and Samsung Galaxy Tab 3 the first day, but when I tried it today the S2 doesn't load the location and the receiver never gets fired, although I didn't change my code and the settings are exactly the same. I also rebooted my phone and reinstalled the Application. Still no difference, but it is still working on the tablet.

I noticed that there are many problems wit the Location API reported in different forums so could it be a bug in Google Location API similar to this: https://code.google.com/p/android/issues/detail?id=57707

Or do I have problems with my settings or my code (posted below):

ProgressBar PBLoad;
@Override 
public void onStart()
{
    super.onStart();
    boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean dataConnection= cm.getActiveNetworkInfo()!=null;
    int resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
      if (!isGPSEnabled||!dataConnection||ConnectionResult.SUCCESS != resultCode) 
      {
          //Show Dialog and ask user to enable GPS or Internet
      }
      else
      {
        registerReceiver(singleUpdateReceiver,new IntentFilter(SINGLE_LOCATION_UPDATE_ACTION));
        Intent updateIntent = new Intent(SINGLE_LOCATION_UPDATE_ACTION);
        singleUpatePI = PendingIntent.getBroadcast(this, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, singleUpatePI);
        PBLoad.setVisibility(View.VISIBLE);
        }
}

And this is the receiver:

protected BroadcastReceiver singleUpdateReceiver = new BroadcastReceiver() 
{
    @Override
    public void onReceive(Context context, Intent intent) {
        context.unregisterReceiver(singleUpdateReceiver);
        String key = LocationManager.KEY_LOCATION_CHANGED;
        Location location = (Location)intent.getExtras().get(key);
        if (location != null) {
            onLocationReceived(location);
        }
        locationManager.removeUpdates(singleUpatePI);
    }
};

Solution

  • Yes, it turned out that it was a problem of the google api. I waited some hours and restarted the device and then it worked.