Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 067902ce authored by Florian Salbrechter's avatar Florian Salbrechter
Browse files

Fix bug in IntentService and Service example code.

Use Thread.sleep instead of Object.wait which simplifies the example.
Also there is a tiny chance, that wait will be called with 0, which
blocks forever.

Change-Id: I4cf90a33089a64bdf802620350f76af08f15f721
(cherry picked from commit 77d8857e)
parent bc56ca34
Loading
Loading
Loading
Loading
+10 −16
Original line number Original line Diff line number Diff line
@@ -336,14 +336,11 @@ public class HelloIntentService extends IntentService {
  protected void onHandleIntent(Intent intent) {
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
      // For our sample, we just sleep for 5 seconds.
      long endTime = System.currentTimeMillis() + 5*1000;
      while (System.currentTimeMillis() < endTime) {
          synchronized (this) {
      try {
      try {
                  wait(endTime - System.currentTimeMillis());
          Thread.sleep(5000);
              } catch (Exception e) {
      } catch (InterruptedException e) {
              }
          // Restore interrupt status.
          }
          Thread.currentThread().interrupt();
      }
      }
  }
  }
}
}
@@ -405,14 +402,11 @@ public class HelloService extends Service {
      public void handleMessage(Message msg) {
      public void handleMessage(Message msg) {
          // Normally we would do some work here, like download a file.
          // Normally we would do some work here, like download a file.
          // For our sample, we just sleep for 5 seconds.
          // For our sample, we just sleep for 5 seconds.
          long endTime = System.currentTimeMillis() + 5*1000;
          while (System.currentTimeMillis() < endTime) {
              synchronized (this) {
          try {
          try {
                      wait(endTime - System.currentTimeMillis());
              Thread.sleep(5000);
                  } catch (Exception e) {
          } catch (InterruptedException e) {
                  }
              // Restore interrupt status.
              }
              Thread.currentThread().interrupt();
          }
          }
          // Stop the service using the startId, so that we don't stop
          // Stop the service using the startId, so that we don't stop
          // the service in the middle of handling another job
          // the service in the middle of handling another job