android xamarin code
lockctl = ((PowerManager)GetSystemService(Android.Content.Context.PowerService)).NewWakeLock(
WakeLockFlags.Partial, "tag"); //cpu on
lockctl.Acquire();
WifiManager wifi = ((WifiManager)GetSystemService(Android.Content.Context.WifiService));
lockwifi = wifi.CreateWifiLock(Android.Net.WifiMode.Full, "wifilock");
lockwifi.SetReferenceCounted(true);
lockwifi.Acquire();
`
I am making a cctv app. The problem is that the screen turns off after a day.
I want to keep this screen on.
I want the screen to stay on even after several days
Here is the code you can use to keep the screen awake and never goes off while your application is running.
getWindow(). addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
This is one of the flag we can set to make the screen awake.
Put this inside your activity ex:- MainActivity inside onCreate().
If your app is running foreground, you can keep the screen on by add the following code into the each activity's OnCreate method of your project:
protected override void OnCreate(Bundle savedInstanceState)
{
....
this.Window.AddFlags(Android.Views.WindowManagerFlags.KeepScreenOn);
}
Or you can just add the android:keepScreenOn="true" in the every layout.xml file, such as:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:keepScreenOn="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
For more information, you can check the offical document about the keeping screen on.
Related
I do not know if the title is correct, here is what happens.
I have an application which works differently on a phone and on a tablet, on a phone it shows as portrait on a tablet it shows as landscape.
To achieve this I created a class called CoreActivity which is extended by all my activities and does the following:
public class CoreActivity extends Activity {
protected boolean _landscape = false;
public boolean isPhone() {
int layoutSize = getScreenLayoutSize();
return (layoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || layoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isPhone() && !_landscape) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
protected int getScreenLayoutSize() {
return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
}
}
My problem occurs when I wish to show a screen on a phone setup on landscape mode, to do this I use the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
_landscape = true
super.onCreate(savedInstanceState);
}
The problem is, that on a phone, if the user is holding the phone on portrait mode (as one would, since most of the application is in portrait mode) then the activity gets created and destroyed and then recreated. But if they are holding it on landscape mode, then it is only created once.
My problem occurs because on the onCreate method I launch the threads that load data, and I also show fragments.
Is there a way to avoid this problem? is there a way to launch an activity from the start on portrait mode and not change it, or have it not create twice?
Thanks in advance for any help you can provide
Recreating occurs just because you are forcing to, by calling setRequestedOrientation probably in order to set screen orientation. However you don't need to check and change it by code. You can do it via xml file. You can set different xml files depending on the screen size. But it is little bit hack so there is no guarantee in the future.
On the manifest file you can force by:
<activity
android:name="com.my.example.MyActivity"
android:screenOrientation="landscape"/> // or portrait
So as far as I understand you want to force portrait for small sizes and landscape(or sensor) for larger screen sizes. But above configuration applies for all screen sizes. Here is the tricky part: landscape portrait sensor etc. are all integers defined here.
As you might guess you can write android:screenOrientation=0 instead of landscape. What we know from beginning lessons of android, we can define integers in xml files then their values might vary on screen size. So..
You should first create different integers.xml files for different screen sizes. i.e.:
values
- integers.xml
values-large
- integers.xml
for values/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="orientation">1</integer> // 1 for portrait
</resources>
for values-large/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="orientation">0</integer> // 0 for landscape
</resources>
and finally you you have to manipulate manifest file by:
<activity
android:name="com.my.example.MyActivity"
android:screenOrientation="#integer/orientation"/> // read constant from xml files
You can also use sensor , fullSensor, nosensor, locked, behind, reverseLandscape, reversePortait options too. But I am warning you, this is a hack solution.
Well, I found a solution to this issue, just declare:
android:screenOrientation="locked"
on every activity having this issue in the manifest.
And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,
It will work! ;)
If all your activites must have the same orientation on a device, you can start your application with a splash screen activity, set the orientation like in your current code and forward to your CoreActivity or any other activity in your App.
In your AndroidManifest.xml set
<activity
android:name=".CoreActivity"
android:screenOrientation="behind"/>
This will use the same orientation as the activity that's immediately beneath it in the activity stack.
Did you try adding android:configChanges="keyboard|keyboardHidden|orientation" to the <activity>-Tag inside of your AndroidManifest.xml?
This should prevent the system from restarting the Activity, but I am not sure whether this will actually work when forcing the orientation programatically. It's worth a shot though.
try this in your manifest.xml...
this will stop the recalling the onCreate() multiple time while orientation changes...
android:configChanges="keyboardHidden|orientation|screenSize"
You dont need to handle this manually. Android has built it support for different screen sizes
check this link
http://developer.android.com/training/multiscreen/screensizes.html
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 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.
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)
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.