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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Newbie
  • 1
  • 3

1 Answers1

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>
Muhammad Farhan
  • 1,113
  • 10
  • 22
  • 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. – Newbie Jan 25 '20 at 05:44
  • @Newbie can you please provide the error you're getting? – Muhammad Farhan Jan 25 '20 at 06:02