Reputation: 1586
I've been trying to work out how to keep the screen on in an app I'm working on. There is a lot of information about this online, but I haven't really found anything flutter specific. I've found various post about using wakelocks but when I try that my app always ends up crashing on start. I'd prefer not to use wakelocks though.
The information that I find tells me to put the following into MainActivity.java.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
When I do this, the app won't compile because of errors with it.
package WindowManager does not exist
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
^
1 error
FAILURE: Build failed with an exception.
This is the code that I have in MainActivity.java
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
GeneratedPluginRegistrant.registerWith(this);
}
Is there any advice on what I'm missing?
Edit: I was able to get FLAG_KEEP_SCREEN_ON working. I needed to import android.view.WindowManager in MainActivity.java. This is what the code looks like now:
import android.os.Bundle;
import android.view.WindowManager; //Needed for not letting screen shut off.
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
//Do not let screen shut off.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
I tried using android:keepScreenOn="true", but could not work out where to put it in my files.
Upvotes: 2
Views: 2510
Reputation: 1
You should add this in manifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK" />
This permission will allow you to use wake lock api.
Upvotes: 0
Reputation: 704
I think you can use android:keepScreenOn="true". Please take a look at https://developer.android.com/training/scheduling/wakelock
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
Upvotes: 1