0

My app has an Intent Service which keeps checking on a web server if there is "start" or "stop" word on an html page every 100000ms.

If it receives "start" it starts a locationmanager.requestUpdateLocation(..), if it receives "stop" it stops it with locationmanager.removeUpdates(..).

But I've got this problem:

When I call locationmanager.requestUpdateLocation(..) I NEVER enter in my listener's onLocationChanged (that's implemented in the same scope of service).

Then, I observe that my Intent service doesn't live forever :( I put a receiver which captures the moment when Android boots up and starts it (and this work) and a ploy in which when my Intent service is closed by user it restarts himself :P

But it never lives forever, after few hours (or maybe only 30 minutes) it dies :(

Why?

Here's the code:

@Override
protected void onHandleIntent(@Nullable Intent intent) {

  Log.i("Service", "Started");

  LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
  LocationListener  locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
    //IT NEVER ENTER HERE 
    Log.i("LOC", "LAT: " + location.getLatitude() +" LONG: "+location.getLongitude());
    }

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

    public void onProviderEnabled(String provider) {
    }

    public void onProviderDisabled(String provider) {
    //NEVER ENTER ALSO HERE
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    }
  };

  int localizzazioneAttiva=0;
  int delay = 0;

  while (true) {
    Log.i("SERVICE_CheckSito", "Check Sito STARTED");

    String inputLine = "";
    StringBuffer stringBuffer = new StringBuffer();
    //Check Permission and SKD_INT
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED)
    {
      if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {

        URL url;
        try
        {
          url = new URL("http://www.sito.com/file.html");
          HttpURLConnection httpURLConnection;
          httpURLConnection = (HttpURLConnection) url.openConnection();
          httpURLConnection.connect();
          if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

            try {
              while ((inputLine = bufferedReader.readLine()) != null) {
                stringBuffer.append(inputLine).append("\n");
              }
            } finally {
              bufferedReader.close();
            }
          }
        } catch (IOException e) {
        //  e.printStackTrace();
        }
      } else return;
    }

    if (stringBuffer.toString().equals("start\n") && 0==localizzazioneAttiva )
    {
      localizzazioneAttiva=1;
      delay = 1000;
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 0, locationListener);
    }
    if(stringBuffer.toString().equals("stop\n")) {
      delay = 2000;
      locationManager.removeUpdates(locationListener);
      localizzazioneAttiva=0;
    }

    Log.i("SERVICE_CheckSito", "Message from server: " + stringBuffer.toString());
    Log.i("SERVICE_CheckSito", "Check Sito FINISHED");
    try {
      Thread.sleep(delay);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
Rodia
  • 1,407
  • 8
  • 22
  • 29
Andrea
  • 23
  • 1
  • 6

1 Answers1

0

I should not answer after 3 beers. My bad, did not see you appending /n.

Consider using a plain Service and the FusedLocationProvider.

  • Mmm I use Intent Service because I need to use "Thread.sleep(100000)" sometime! Do you think that if I use normal Service I resolve my problem? I already try to use a real device ;) If you have some problem with AVD and You're using android studio go to Tools, Android, Android Device Monitor. Select your virtual device (only if there's no app in debug mode) and You can set your lat and long! ;) – Andrea Mar 31 '17 at 18:56
  • Setting lat/long in the device manager never triggered onlocationchanged for me. Im at a bar right now, checking your code later on. Anyways i wish you gold luck, i myself had some really annoying issues using locationmanager – Patrick Bruns Mar 31 '17 at 19:23
  • In Emulator open Extended controls and change your location. This cause that onLocationChanged method is called – Honza Musil Mar 31 '17 at 19:27
  • Did that. Did not call onlocchanged. Perhaps its a bug in my end – Patrick Bruns Mar 31 '17 at 19:30