Problem :
I am testing android application using robotium, My Problem is screen gets off after few second if user did not interact with the app,how to keep screen always on for testing app?
What I tried :
Made Stay awake on under development options of avd
I cross verified this post but it was related to development side where i cant modify the source of apk
My Question :
how to keep screen always on for avd testing app ?
For testing you can instantiate a WakeLock.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
wakeLock.acquire();
Don't forget the permisson WakeLock
<uses-permission android:name="android.permission.WAKE_LOCK" />
Alternatively you can try the "wake lock" option in the devices development settings.
Window w = ((Activity)mContext).getWindow(); // in Activity's onCreate() for instance
w.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
Related
I runs web server in my rooted Android Tablet. I setup it for web development. I created a home network by this Android server. but when the screen of the tab turns off, the server also stop working & again start working when i turn on the screen. but its not possible to turn on the screen for long time. I can be harmful for my tablet. Is there any way to keep awake my tablet when the screen is turn off so that my server can work properly in Background.
please help
You can run a service in the background and acquire lock like this :
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
and return onStartCommand START_STICKY. To release the wake lock, call wakelock.release().
Do not forget to put the permission in the manifest file :
<uses-permission android:name="android.permission.WAKE_LOCK" />
Or you can use this app to prevent phone from sleeping :
https://play.google.com/store/apps/details?id=nl.syntaxa.caffeine
In my application, I use AsyncTask to download large files (300M+). I noticed that when the user turns off their screen (locks their device), the wifi will disconnect and the download will hang.
I wonder if it is possible to avoid this?
You required to implement WakeLock in your application. Wakelock will wake up the CPU incase of screen is off and performs the operations in normal ways.
Write down following code before starting the AsyncTask,
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
You need to write wl.release(); on PostExecution() method. And you need to define permission in AndroidManifest.xml as follows,
<uses-permission android:name="android.permission.WAKE_LOCK" />
The common practice in this case is to take a WakeLock to keep the CPU awake, and take a WifiLock to keep the wifi connected, so that your app keeps running even if the screen is turned off.
Don't forget to release the locks when you're done!
I have a simple battery test app I wrote for Android and have used for several versions now successfully. It works fine in Android 4.1 The app simply launches the chrome browser to a different web site, logs the time to a database, waits 60 seconds and does it all over again.
Now, in Android 4.2, the screen turns off and system goes to sleep despite the fact that I have a wakelock and have set screen timeout to never using settings. What's most annoying is it turns off at an indeterminate time, not a fixed time after starting the test. Any idea what I need to do?
Here's the wakelock code I call in the service that launches the browser over and over again.
super.onCreate();
// Wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
I also put this in the layout file
android:keepScreenOn="true"
However, the app's screen itself isn't visible throughout the test as Chrome is always the foreground window and the browser launcher is a service and the wakelock is in the service.
Any idea how to keep the screen from timing out? Nothing seems to work.
Just add following line to your XML file......
android:keepScreenOn="true"
:)
I would guess that Android is killing your service and causing your wakelock to be released. Try making your service run in the foreground (with startForeground()) and see if that helps.
I'm trying to make an App on Android 2.2 that has to run whether the screen is on or off.
I tried realizing it via a Service, but when my phone turns the screen off the service stopps working and I don't understand why.
In my Application I use
startService(new Intent(this, MyService.class));
when the User presses a button
and
Notification notification = new Notification();
startForeground(1, notification);
in the onCreate-Method of the Service class
(I tried it within the onStart() Method, too)
(I also could not find out what the id-field of startForeground() expects so I used the 1)
The service then should start an infinite vibration pattern of the phone so I know whether it is running or not.
But when I turn off the screen, the phone stops vibration immediately
Please help me. I don't know how I can fix that (and google was not a big help)
Sincerely
zed
Android devices go to a sleep mode when idle (e.g. when the screen is off), in order to conserve the battery.
To keep your service running you need to aquire a WakeLock. There are plenty of tutorials how to use it, like this one.
Note that having a service running all the time will drain your battery. Also make absolutely sure to release the wakelock when not needed, otherwise you're phone will always be awake.
Try returning 'START_STICKY' from 'onStartCommand() '. This will keep the service running, restarting it if necessary, without keeping the screen on.
Have a look at http://developer.android.com/reference/android/app/Service.html#START_STICKY
try this
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
and in manifest file
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
so when your notification or alarm manager service running, screen will light on and the keyguard will unlock.I wish it works for who has this problem too.
I have an Android (version 1.5) application which needs to be constantly running when a button is pressed. So, when a Button is pressed I would like the phone to remain on and not want the screen or CPU to time-out.
When another Button is pressed I would like the phone to be back to normal and time-out as per user settings.
Update: As suggested by Steve Pomeroy, this might be a better way to do it.
You can use a WakeLock that requires the following permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Here is how you aquire and release a WakeLock:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
// wake locked...
wl.release();
Depending on your requirements you might be able to use a different type of WakeLock.
Instead of using a wakelock, you should consider the solution proposed here: Force Screen On
It is much easier to use and doesn't have the potential of accidentally wasting the user's batteries.