Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class WakeLockSnippetsJava extends Activity {

PowerManager.WakeLock wakeLock;
final long WAKELOCK_TIMEOUT = 10*60*1000L; // 10 minutes

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -18,7 +19,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock =
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag");
wakeLock.acquire();
wakeLock.acquire(WAKELOCK_TIMEOUT);
// [END android_backgroundwork_wakelock_create_java]

super.onCreate(savedInstanceState);
Expand All @@ -28,7 +29,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
// [START android_backgroundwork_wakelock_release_java]
void doSomethingAndRelease() throws MyException {
try {
wakeLock.acquire();
wakeLock.acquire(WAKELOCK_TIMEOUT);
doTheWork();
} finally {
wakeLock.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
package com.example.snippets.backgroundwork

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.PowerManager

// Snippets for doc page go here
@Suppress("unused_parameter")
class WakeLockSnippetsKotlin : Activity() {

val WAKELOCK_TIMEOUT = 10 * 60 * 1000L // 10 minutes
// [START android_backgroundwork_wakelock_create_kotlin]
val wakeLock: PowerManager.WakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
(getSystemService(POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag").apply {
acquire()
acquire(WAKELOCK_TIMEOUT)
}
}
// [END android_backgroundwork_wakelock_create_kotlin]
Expand All @@ -44,7 +44,7 @@ class WakeLockSnippetsKotlin : Activity() {
fun doSomethingAndRelease() {
wakeLock.apply {
try {
acquire()
acquire(WAKELOCK_TIMEOUT)
doTheWork()
} finally {
release()
Expand Down