How to implement partial wakeLock correctly?
Asked Answered
S

1

0

I have found ways to implement it as follows...

code:

public class main extends AppCompatActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lvl_1);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AppName: tag");
    wl.acquire();
    wl.release();
}

And then I have also added the permission code for wake lock in manifest. However, it does not work properly, as the screen goes off, when I reopen the screen again, my app closes and some random error occurs.(the error is not related to wakelock, but because of the presence of partial wakelock codes that I have added). Guys, pls help me out. I have been stuck in this issue for almost a week. Thanks in advance, guys.

Shiff answered 23/1, 2020 at 11:34 Comment(1)
why are you releasing it? from docs: "... CPU will be kept on until all partial wake locks have been released."Kalagher
B
0

I strongly recommend you to read this Android Documentation on Doze Mode Keep the device awake

Avoid using Wake Locks if you want to keep the screen on.

If you want to keep the screen on in your Activity use this tag window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) in you onCreate
Or
You can achieve the same behavior by XML Tag. android:keepScreenOn="true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>
Beget answered 23/1, 2020 at 13:36 Comment(2)
Hi, thanks for the answer but that is not what I want. The thing that I want to achieve is that I allow the screen to turn off. Then, when I turn on the screen again, the app remains open. But for now, it exits after screen goes off.Shiff
@Shiff can you please provide the error you're getting?Beget

© 2022 - 2024 — McMap. All rights reserved.