Skip to content

Commit ab3fd15

Browse files
committed
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/
1 parent f8abcf7 commit ab3fd15

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-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_kotlin]
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.snippets.backgroundwork
2+
3+
import android.app.Activity
4+
import android.content.Context
5+
import android.os.Bundle
6+
import android.os.PowerManager
7+
8+
// Snippets for doc page go here
9+
class WakeLockSnippetsKotlin : Activity() {
10+
11+
// [START android_backgroundwork_wakelock_create_kotlin]
12+
val wakeLock: PowerManager.WakeLock =
13+
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
14+
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag").apply {
15+
acquire()
16+
}
17+
18+
}
19+
// [END android_backgroundwork_wakelock_create_kotlin]
20+
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
23+
24+
super.onCreate(savedInstanceState)
25+
}
26+
27+
// [START android_backgroundwork_wakelock_release_kotlin]
28+
@Throws(MyException::class)
29+
fun doSomethingAndRelease() {
30+
wakeLock.apply {
31+
try {
32+
acquire()
33+
doTheWork()
34+
} finally {
35+
release()
36+
}
37+
}
38+
}
39+
// [END android_backgroundwork_wakelock_release_kotlin]
40+
41+
private fun doTheWork() {
42+
43+
}
44+
45+
}

0 commit comments

Comments
 (0)