Skip to content

Commit 79a26f9

Browse files
authored
Adding code snippets for wake locks doc (#582)
* Adding code snippets for wake locks doc Starting with the code snippets to add and release a wake lock, we can add other snippets later. Used by docs in: https://developer.android.com/develop/background-work/background-tasks/awake/wakelock/ After this is submitted, I'll follow with a CL to update the published docs.
1 parent a87b076 commit 79a26f9

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.snippets.backgroundwork;
2+
3+
/**
4+
* Placeholder exception used by wake lock docs.
5+
*
6+
* Existing wake lock code snippets inclde a method that throws "MyException", I need to define
7+
* it for the code snippets to use.
8+
*/
9+
public class MyException extends RuntimeException {
10+
public MyException(String message) {
11+
super(message);
12+
}
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.snippets.backgroundwork;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.os.PowerManager;
6+
7+
import androidx.annotation.Nullable;
8+
9+
public class WakeLockSnippetsJava extends Activity {
10+
11+
PowerManager.WakeLock wakeLock;
12+
13+
@Override
14+
protected void onCreate(@Nullable Bundle savedInstanceState) {
15+
16+
// [START android_backgroundwork_wakelock_create_java]
17+
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
18+
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag");
19+
wakeLock.acquire();
20+
// [END android_backgroundwork_wakelock_create_java]
21+
22+
super.onCreate(savedInstanceState);
23+
}
24+
25+
// [START android_backgroundwork_wakelock_release_java]
26+
void doSomethingAndRelease() throws MyException {
27+
try {
28+
wakeLock.acquire();
29+
doTheWork();
30+
} finally {
31+
wakeLock.release();
32+
}
33+
}
34+
// [END android_backgroundwork_wakelock_release_java]
35+
36+
private void doTheWork() {
37+
}
38+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 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+
* https://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.snippets.backgroundwork
18+
19+
import android.app.Activity
20+
import android.content.Context
21+
import android.os.Bundle
22+
import android.os.PowerManager
23+
24+
// Snippets for doc page go here
25+
class WakeLockSnippetsKotlin : Activity() {
26+
27+
// [START android_backgroundwork_wakelock_create_kotlin]
28+
val wakeLock: PowerManager.WakeLock =
29+
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
30+
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag").apply {
31+
acquire()
32+
}
33+
}
34+
// [END android_backgroundwork_wakelock_create_kotlin]
35+
36+
override fun onCreate(savedInstanceState: Bundle?) {
37+
38+
super.onCreate(savedInstanceState)
39+
}
40+
41+
// [START android_backgroundwork_wakelock_release_kotlin]
42+
@Throws(MyException::class)
43+
fun doSomethingAndRelease() {
44+
wakeLock.apply {
45+
try {
46+
acquire()
47+
doTheWork()
48+
} finally {
49+
release()
50+
}
51+
}
52+
}
53+
// [END android_backgroundwork_wakelock_release_kotlin]
54+
55+
private fun doTheWork() {
56+
}
57+
}

0 commit comments

Comments
 (0)