@ThreadSafe public final class PartialWakeLockForService extends Object
Service in a thread-safe
manner. The recommended way to use this class is as follows:
PartialWakeLockForService within the
Service subclass that needs a WakeLock.beforeStartingService(Context) and Context.startService(Intent).beforeDoingWork(Context), and
afterDoingWork(Context) at the appropriate times.
This class handles scenarios where beforeStartingService(Context) isn't always called
(for example, the Service is restarted due to Service.START_STICKY or
similar).
Clients of this class must have the permission Manifest.permission.WAKE_LOCK.
| Constructor and Description |
|---|
PartialWakeLockForService(String name)
Construct a new WakeLock helper.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterDoingWork(Context context)
Call to issue after the service completes its work.
|
void |
beforeDoingWork(Context context)
Call to issue within prior to starting the service's work, typically in either
Service.onStartCommand(android.content.Intent, int, int) or
IntentService.onHandleIntent(Intent). |
void |
beforeStartingService(Context context)
Call to issue immediately before calling
ContextWrapper.startService(android.content.Intent). |
String |
toString() |
public PartialWakeLockForService(String name)
name - Name of the WakeLock, used for debugging. Cannot be the empty string.public void beforeStartingService(Context context)
ContextWrapper.startService(android.content.Intent).context - Application context.public void beforeDoingWork(Context context)
Service.onStartCommand(android.content.Intent, int, int) or
IntentService.onHandleIntent(Intent).context - Application context.public void afterDoingWork(Context context)
This method MUST always be called, otherwise locks obtained previously by
beforeStartingService(Context) or beforeDoingWork(Context) may never be
released. Typically, this should be placed in a finally block within
Service.onStartCommand(android.content.Intent, int, int) or
IntentService.onHandleIntent(Intent).
context - Application context.