File tree Expand file tree Collapse file tree 3 files changed +96
-0
lines changed
misc/src/main/java/com/example/snippets/backgroundwork Expand file tree Collapse file tree 3 files changed +96
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments