Android How To Keep Screen On While Using webView
android, java
Solution
There are 3 possible ways of doing this, two are preferable and similar in function it’s just personal preference which you use. The other is a bit more aggressive and can be used in other scenarios where you want other parts of the system to stay awake as well (like the processor).
Here’s the ways:
Inform the `window manager` in `onCreate` you want the screen to stay on
This Activity below keeps the screen on using the window manager, you don’t have to worry about managing this it will be kept on for the duration of the Activity lifecycle. The screen may dim but it won’t turn off. No permissions are needed in your manifest.
package com.mysite;
import com.mysite.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebView;
public class MainActivity extends Activity {
public WebView myWebView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
myWebView = new WebView(this);
myWebView.loadUrl("http://www.mysite.com");
}
}
`WakeLock` – used for critical downloads or things that you definitely don’t want the Android system shutting down for
This Activity aquires a wakelock to keep the screen on whilst in this activity. This requires a permission in your manifest. It is important to manage your wakelocks and always release them when finished (in onPause).
public class MainActivity extends Activity {
private static final String TAG = "com.mysite.ScreenOnWakeLockActivity.WAKE_LOCK_TAG";
private WakeLock wakeLock;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG);
myWebView = new WebView(this);
myWebView.loadUrl("http://www.mysite.com");
}
@Override
protected void onResume() {
super.onResume();
wakeLock.acquire();
}
@Override
protected void onPause() {
super.onPause();
wakeLock.release();
}
}
Declare the screen stays on in your `XML layout`
This activity keeps the screen on using a flag in the XML layout file we are using `android:keepScreenOn=”true”` no manifest permissions are needed.
/**
* This activity keeps the screen on using a flag in the XML layout file we are using 'android:keepScreenOn="true"'
* We don't use a wakelock so no manifest permissions are needed
*
* @author chirag.saga
*
*/
public class MainActivity extends Activity {
public WebView myWebView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
myWebView = new WebView(this);
myWebView.loadUrl("http://www.mysite.com");
}
}
Problem
First, I am new to Android and Java programming. That said I am not sure if what I want to do can be done. I would like to be able to use webView to load a url and use keep_screen_on to prevent the screen from dimming. Below is the code that I am using. I am able to load the webpage but the screen dims after a minute or so. Any help is appreciated. This is the Java page: ``` package com.mysite; import com.mysite.R; import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; import android.webkit.WebView; public class MainActivity extends Activity { public WebView myWebView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flag); myWebView = new WebView(this); myWebView.loadUrl("http://www.mysite.com"); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } ``` This is my Manifest page: ``` <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mysite" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mysite.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ```