Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.supportv4.content;
     18 
     19 //BEGIN_INCLUDE(complete)
     20 import android.app.IntentService;
     21 import android.content.Intent;
     22 import android.os.SystemClock;
     23 import android.util.Log;
     24 
     25 public class SimpleWakefulService extends IntentService {
     26     public SimpleWakefulService() {
     27         super("SimpleWakefulService");
     28     }
     29 
     30     @Override
     31     protected void onHandleIntent(Intent intent) {
     32         // At this point SimpleWakefulReceiver is still holding a wake lock
     33         // for us.  We can do whatever we need to here and then tell it that
     34         // it can release the wakelock.  This sample just does some slow work,
     35         // but more complicated implementations could take their own wake
     36         // lock here before releasing the receiver's.
     37         //
     38         // Note that when using this approach you should be aware that if your
     39         // service gets killed and restarted while in the middle of such work
     40         // (so the Intent gets re-delivered to perform the work again), it will
     41         // at that point no longer be holding a wake lock since we are depending
     42         // on SimpleWakefulReceiver to that for us.  If this is a concern, you can
     43         // acquire a separate wake lock here.
     44         for (int i=0; i<5; i++) {
     45             Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
     46                     + "/5 @ " + SystemClock.elapsedRealtime());
     47             try {
     48                 Thread.sleep(5000);
     49             } catch (InterruptedException e) {
     50             }
     51         }
     52         Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
     53         SimpleWakefulReceiver.completeWakefulIntent(intent);
     54     }
     55 }
     56 //END_INCLUDE(complete)
     57