0

I am working on a prototype Android app that sends gps data to a server. I had something working when I called my class GetLocation directly from my MainActivity class. However, I would want this to really work as a background service. I tried using similar code, but for some reason the LocationListener's functions never get called (the only one I'm looking at is the onLocationChanged). Am I misunderstanding how this works? Thanks. Here's my code.

public class GetLocation {
...
public GetLocation(Context myContext, String hostParam)
{
    this.context = myContext;
    sendMessage = false;
    sendData = new SendData(Constants.HOST_NAME);
    locationManager = (LocationManager) context.getSystemService((Context.LOCATION_SERVICE));
    this.host = hostParam;
    locationListener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location)
        {
            String sentence = AssembleSentence.assembleNMEAURL(location);
            //sendData.sendMessage(sentence);
            int geozoneLocation = GeoZone.getGeozoneLocation(location);
            switch(geozoneLocation)
            {
                case GeoZone.LEVEL_ONE:
                    sendMessage = false;
                    if(Constants.LEVEL_ONE_INTERVAL != currentInterval)
                    {
                        startLocationUpdates(Constants.LEVEL_ONE_INTERVAL, LocationManager.NETWORK_PROVIDER);
                    }
                    break;
                case GeoZone.LEVEL_TWO:
                    sendMessage = false;
                    if(Constants.LEVEL_ONE_INTERVAL != currentInterval)
                    {
                        startLocationUpdates(Constants.LEVEL_TWO_INTERVAL, LocationManager.NETWORK_PROVIDER);
                    }
                    break;
                case GeoZone.LEVEL_THREE:
                    sendMessage = false;
                    if(Constants.LEVEL_ONE_INTERVAL != currentInterval)
                    {
                        startLocationUpdates(Constants.LEVEL_THREE_INTERVAL, LocationManager.NETWORK_PROVIDER);
                    }
                    break;
                case GeoZone.LEVEL_FOUR:
                    sendMessage = true;
                    if(Constants.LEVEL_ONE_INTERVAL != currentInterval)
                    {
                        startLocationUpdates(Constants.LEVEL_FOUR_INTERVAL, LocationManager.GPS_PROVIDER);
                    }
                    break;
            }
            if(sendMessage)
            {
                sendData.sendMessage(sentence);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

        @Override
        public void onProviderDisabled(String provider)
        {

        }
    };
}
....
}

Which is called by my Service class below.

public class TelematicsIntent extends Service
{

public TelematicsIntent()
{
}
@Override
public IBinder onBind(Intent intent)
{
    throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate()
{

    GetLocation location = new GetLocation(this, Constants.HOST_NAME);
}


@Override
public void onDestroy()
{

}
}

And this Service class is called by the MainActivity class. I know that the GetLocation class is being called correctly, as I set up a break point in the this.host = hostParam and the code stopped there, but I put another break point in the statement int geozoneLocation = GeoZone.getGeozoneLocation(location); where GeoZone class is another class that just goes through the location data and returns whether the location is inside a particular area. Any ideas?

user2850818
  • 345
  • 1
  • 3
  • 17
  • Why are you not using Gyroscope or Accelerator? – We're All Mad Here Oct 14 '15 at 19:41
  • I don't really need any acceleration or orientation information. The stuff I need I was able to get from the Location object when I got this to work in another try I was doing. – user2850818 Oct 14 '15 at 19:45
  • Try this Question http://stackoverflow.com/questions/32290045/error-invoke-virtual-method-double-android-location-location-getlatitude-on – KISHORE_ZE Oct 14 '15 at 19:48
  • Okay, so to understand this, LocationListener's functions never get called when your app is in the background or even when the user is in the interface as well? – We're All Mad Here Oct 14 '15 at 19:48
  • Correct. The LocationListener's functions never get called. And, I'm not getting any type of error in the logcat. And I would much rather keep things as seperate as possible, so I would not like to implement the LocationListener in the MainActivity. – user2850818 Oct 14 '15 at 19:50

1 Answers1

0

I figured out why it wasn't being called. I forgot to actually start the location services, as I had no way in the code for them to actually start. I should have seen this earlier, but I guess I didn't.

user2850818
  • 345
  • 1
  • 3
  • 17