Force Screen On - android

How do I force the screen to stay active and not shut off while my app is running?

PLEASE DO NOT USE A WAKE LOCK
This requires that you give your app an additional permission, and it is very easy to introduce bugs where you accidentally remain holding the wake lock and thus leave the screen on.
It is far, far better to use the window flag FLAG_KEEP_SCREEN_ON, which you can enable on your activity's window in your onCreate() like this:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
This will make sure that the screen stays on while your window is in the foreground, and only while it is in the foreground. It greatly simplifies this common use case, eliminating any juggling you need to do as your app transitions between states.

This Question has Already Great Answer by #hackbod !
I am Answering this Question with Two Additional Solutions !
Existing Solution :
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Additional Solutions:
we can use keepScreenOn
1. implementation using setKeepScreenOn() in java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// or any View (in case generated programmatically )
View v = getLayoutInflater().inflate(R.layout.driver_home, null);
v.setKeepScreenOn(true);
setContentView(v);
}
Docs http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
2. Adding keepScreenOn to xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
Docs http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
Note ( Some Useful Points) :
1. it Doesn't matter that keepScreenOn should be used on Main/Root/Parent View it can be used with any child view will work As same as it works in Parent view
2. The Thing Only matter is that View's Visibility must be visible other wise it will not work !

Another solution is to add android:keepScreenOn="true" (documentation) to the views that need to keep the screen on.
Allows for a little bit more granular control in terms of which views stay on and which don't. You can even reference a setting from a resource file this way.

Related

Android Activity handling with both orientation

As a beginner android developer I have faced with the problem concerning the fragments and activities handling when screen orientation is changed.
This is my situation:
I have two fragments (F_A and F_B). F_A contains ListView (LV), and F_B contains combination of several Views: Description of LV (lets call it as Desc).
When app runs in portrait mode first activity shows LV and after clicking on an item the second activity runs. In this case when the orientation of the screen is changed from portrait to landscape mode I want to change the layout to show both fragments as it is done when the app initially runs in landscape mode.
I hope the provided pieces of codes will not confuse my description of the situation more :)
first activity
main_activity.java file contains:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // there is two actitivy_main.xml files
// one in layout/ directory and the other one in layout-land/ directory.
...
}
#Override
public void respond (int index) {
// this method got the second fragment if it is visible and not null.
// otherwise starts new activity for only description fragment (F_B)
}
second activity (for portrait mode only)
DescriptionActivity.java file contains:
#Override
protected void onSaveInstanceState(Bundle outState) {
// saving some stuff for further handling when orientation will changed from port to land.
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_description); // This layout contains only F_B fragment
... //here some initialization is done for the description.
}
Considering that during the screen rotation the Life Cycle guard methods such as onPause(), onStart() etc. are called I think that I have to check the orientation in that methods then destroy the second activity and run the first: main_activity which will itself check what layout should be shown (in this case the layout from layout-land/ directory, I am not sure weather this is a good idea, so I think to ask for help to developers with more experience.
Many thanks in advance,
Arsen
The best way to achieve what you want is described in Fragments Android documentation, there you have an example with the full explanation.
But just as an advice, you don't have to use 2 diff activities nor check the orientation of the device, you just have to create an activity_main.xml for diff configurations in layout-port (portrait layout) and layout-land (landscape layout) you can learn how in here: Supporting Different Screens and Orientations. I know it's kind of hard at the beginning but working with fragments is one of the best practices for android. Read those tutorials and you'll be able to achieve your goal. Good luck :)

Android WebView re-rendering UI after changing orientation.

In My application i am setting orientation of the application on button click, using setRequestedOrientation(), My application has one webview which displays local web page.
I am using solution as mentioned in below link, to stop reloading of page when orientation changes.
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
Only difference is that i am not having onSaveInstanceState and onRestoreInstanceState in my activity.
What happens in my case is that, my web view does not reload when i change the orientation but it just re-renders the UI upon changing the orientation.
It happens like it first displays the UI and then it displays the white blank screen for few seconds and then again it displays UI.
How can i workaround it?
Once upon a time I used their implementation however I after reading up on the Android documentation I realized that much of their code was pointless since registering for config changes (in the manifest) means that your activity won't be destroyed as it normally is.
Long story short, you don't need to use a FrameLayout as a container for your webView, you may not need onConfigurationChanged, etc. If I get some time I will post some code as an example.
For now, make sure you have the following in your manifest file
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
instead of
android:configChanges="keyboard|keyboardHidden|orientation"
since on orientation change devices may get get a screenSize change
EDIT This is what I have done (skimmed from one of my projects so I may have missed minor details)
in AndroidManifest.xml
<activity android:name=".Activities.WebWrapperActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>
web_wrapper_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView android:id="#+id/web_wrapper_activity_web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="outsideOverlay"/>
</LinearLayout>
The Activity (or a portion of it)
public class WebWrapperActivity extends Activity {
private WebView _webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_wrapper_activity_layout);
init();
}
private void init() {
_webView = (WebView) findViewById(R.id.web_wrapper_activity_web_view);
_webView.setScrollbarFadingEnabled(true);
_webView.getSettings().setLoadsImagesAutomatically(true);
_webView.getSettings().setJavaScriptEnabled(true);
_webView.getSettings().setAllowFileAccess(true);
_webView.getSettings().setSavePassword(false);
_webView.getSettings().setPluginState(WebSettings.PluginState.ON);
_webView.loadUrl(“http://www.google.com”);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
_webView.saveState(outState);
}
}
As i mentioned previously, you may not need onConfigurationChanged if your webView is the only view in the Activity.
If you have any issues let me know

How to deactivate stand by mode in Android? [duplicate]

How do I force the screen to stay active and not shut off while my app is running?
PLEASE DO NOT USE A WAKE LOCK
This requires that you give your app an additional permission, and it is very easy to introduce bugs where you accidentally remain holding the wake lock and thus leave the screen on.
It is far, far better to use the window flag FLAG_KEEP_SCREEN_ON, which you can enable on your activity's window in your onCreate() like this:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
This will make sure that the screen stays on while your window is in the foreground, and only while it is in the foreground. It greatly simplifies this common use case, eliminating any juggling you need to do as your app transitions between states.
This Question has Already Great Answer by #hackbod !
I am Answering this Question with Two Additional Solutions !
Existing Solution :
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Additional Solutions:
we can use keepScreenOn
1. implementation using setKeepScreenOn() in java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// or any View (in case generated programmatically )
View v = getLayoutInflater().inflate(R.layout.driver_home, null);
v.setKeepScreenOn(true);
setContentView(v);
}
Docs http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
2. Adding keepScreenOn to xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
Docs http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
Note ( Some Useful Points) :
1. it Doesn't matter that keepScreenOn should be used on Main/Root/Parent View it can be used with any child view will work As same as it works in Parent view
2. The Thing Only matter is that View's Visibility must be visible other wise it will not work !
Another solution is to add android:keepScreenOn="true" (documentation) to the views that need to keep the screen on.
Allows for a little bit more granular control in terms of which views stay on and which don't. You can even reference a setting from a resource file this way.

Keep webview while changing layout on screen rotation

My app have two different layout for portrait and landscape mode which both encapsulate a webview and some buttons. Buttons are at bottom in portrait and at left in landscape so more reading space is available in webview.
The problem is, the activity is recreated on screen rotation and webview loads the first page which is not a wanted behavior.
I searched and found out using android:configChanges="orientation" in activity tag prevents recreating of the activity. But the porblem is it prevents the layout changing too as it happens in activity creation.
I want my program to work in 2.2, waht's the best way to this?
I tested fragments, but dealing with fragment makes things much more complex and the fragment itself needs saving and restoring which may not work in a webview which has javascript state, So I searched more and find a nice article somewhere and with some modification I came to a solution which I suggest:
First, add android:configChanges="orientation|screenSize|keyboard|keyboardHidden" to manifest so app handles the config change instead of android.
Make two different layout for lnadscape and portrait mode and put them in corresponding layout folders. In both layouts instead of webview place a LinerLayout which acts as a placeholder for webview.
In code define initUI method like this and put every thing related to UI initialization in this method:
public void initui()
{
setContentView(R.layout.main);
if (wv == null) wv = new WebView(this);
((LinearLayout)findViewById(R.id.webviewplace)).addView(wv);
findViewById(R.id.home).setOnClickListener(this);
}
If the webview doesn't exist, it will be created and after setContentView(R.layout.main) it will be added to the layout. Other UI customization came afterward.
and in onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
((LinearLayout)findViewById(R.id.webviewplace)).removeAllViews();
super.onConfigurationChanged(newConfig);
initUI();
}
In onConfigChange First the webview is removed from old place holder and initui will be called which will add it back to the new layout.
and in oncreate call initui so the ui will be initialized for the first time.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initUI()
}
Could you not save the last loaded URL in the webview in onSaveInstanceState(Bundle outState) and then make sure to reload it onRestoreInstanceState(Bundle savedInstanceState) using myWebview.loadUrl(restoredUrl)?
edit I know that this might not work if the web page you are displaying requires a state to be kept. But if not it should be a solution to your problem.
From this document https://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
I just edited manifest with android:configchanges so it works fine for me.
<activity android:name=".Webhtml"
android:configChanges="orientation|screenSize"/>

How to close Landscape VideoView Activity properly?

In my application, I have an activity play http live streaming video in landscape mode.
My AndroidManifest.xml:
<activity android:name=".MediaPlayerActivity"
android:label="#string/menu_player"
android:launchMode="singleInstance"
android:screenOrientation="landscape">
</activity>
My activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black">
<VideoView android:id="#+id/myVideoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
</LinearLayout>
The problem is everytime I close this activity (By clicking back button), it always rotate to portrait mode (time is very soon but you can actually see the effect on real device before back to the previous activity) before closing. How can I fix this annoying issue?
Update with more info
This annoying behaviour only happened If the previous activity is in portrait mode, if the previous one is landscape, it is just fine. To me it looks like something to do with Android framework when fade in/out activities with different screenOrientation settings.
Update with the cause
After some deep reading through Google's API, I think I found the reason that cause this annoying behaviour, check out here:
Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).
So what happened behind the scenes when back button is clicked: currnet VideoView Activity (landscape) is destroyed, a new VideoView Activity (portrait) is created due screenOrientation configuration has been changed, and destoryed immidiately (where you can see the effects on screen), last activity in stack is shown. this also explain why this annoying behaviour disappered if last activity has the same screenOrientation settngs.
I am still trying to figure out how to bypass this activity recreation due to configuration change. As it stated in API, overriding
onConfigurationChanged(Configuration), however, since I explicitly define screenOrientation in xml, onConfigurationChanged() is not called, lots of similiar SO has been discussed before, like this one.
Please provide answer on the right direction.
Thanks,
Y
Try adding calls to VideoView's suspend(), resume() and stopPlayback() in your activity's onPause(), onResume() and onDestroy() methods:
#Override
protected void onResume() {
mVideoView.resume();
super.onResume();
}
#Override
protected void onPause() {
mVideoView.suspend();
super.onPause();
}
#Override
protected void onDestroy() {
mVideoView.stopPlayback();
super.onDestroy();
}
The VideoView class implementation varies from one device to another (and the class itself is very sparsely documented), but the Gallery3D code from AOSP does call the above methods in its MovieView activity lifecycle methods, so hopefully most devices should at least make itself looks good in that scenario.
If it still looks bad, you might want to override onBackPressed() in your activity to maybe hide the VideoView or some similar hacks to conceal the annoying behavior :)
Since your activity isn't always recreated if you close one, It may not execute your manifest code always, tough i'm not sure of this.
But something you could try is setting the orientation manually in onResume:
#Override
protected void onResume()
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onResume();
}
this has more on it.
I beleive that a simple solution would to add a field inside your video activity:
boolean isClosing = false;
Set isClosing to true in the onDestroy or, much better, in the onBackPressed (before calling finish())
Now, I supposed you have set onConfigurationChanged to catch orientation changes.
So, in your onConfigurationChanged Encapsulate it with if(!isClosing)
#Override
public void onConfigurationChanged(Configuration newConfig)
{
if(!isClosing){
//YOUR OLD CODE
}
}
You might use a completely different layout for landscape and portrait mode which I recommend, since you use can both differently.
I would recommend the API documentation to this too: http://developer.android.com/guide/topics/manifest/activity-element.html#screen (especially the part about handling savedBundle)
onConfigurationChanged will only be called if you add android:configChanges="orientation" to the activity in the manifest file.
This tells the system that you want to manually handle orientation changes, if you also have android:screenOrientation="landscape", you're also telling the system that the activity should be displayed in landscape mode (they should work together to give you what you want)

Categories

Resources