summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Florian Salbrechter <fsalbrechter@google.com> 2016-02-08 13:36:43 +0000
committer Florian Salbrechter <fsalbrechter@google.com> 2016-02-16 17:37:36 +0000
commit067902ce7ba4ce248d557afe7dedc776442b33b4 (patch)
tree14fbabdb20141820c28d0aca231e6d741abe3822
parentbc56ca34aaa02bc4c1f83d0aad621a1eef45aab8 (diff)
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 77d8857ed58503669e0659989c02fb7f1ca936b4)
-rw-r--r--docs/html/guide/components/services.jd26
1 files changed, 10 insertions, 16 deletions
diff --git a/docs/html/guide/components/services.jd b/docs/html/guide/components/services.jd
index 8da1694b77b5..4053769995b3 100644
--- a/docs/html/guide/components/services.jd
+++ b/docs/html/guide/components/services.jd
@@ -336,14 +336,11 @@ public class HelloIntentService extends IntentService {
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
- long endTime = System.currentTimeMillis() + 5*1000;
- while (System.currentTimeMillis() &lt; endTime) {
- synchronized (this) {
- try {
- wait(endTime - System.currentTimeMillis());
- } catch (Exception e) {
- }
- }
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ // Restore interrupt status.
+ Thread.currentThread().interrupt();
}
}
}
@@ -405,14 +402,11 @@ public class HelloService extends Service {
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
- long endTime = System.currentTimeMillis() + 5*1000;
- while (System.currentTimeMillis() &lt; endTime) {
- synchronized (this) {
- try {
- wait(endTime - System.currentTimeMillis());
- } catch (Exception e) {
- }
- }
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ // Restore interrupt status.
+ Thread.currentThread().interrupt();
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job