How to keep Android awake in background when screen is off - android

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

Related

How to connect Android Application to Internet when the screen is locked?

After some research on Google Search, I tried to find a solution to connect my Android Application to internet when the screen is locked and without the phone charging.
My first impression is that the phone block in background my application. Strange things because others applications on Google Play Store can communicate on the Internet and do things on background.
I have already tried to search solutions on the Internet. My search apparently turn to this link to keep the device awake:
https://developer.android.com/training/scheduling/wakelock
But this solution did not work properly for me and the android system block my application in background when the screen is locked. If the screen is unlocked, the application works properly.
Moreover, I do not understand the problem. Probably, the android energy saver block my application.
Have you any idea to fix this? And if not, how to use the wakeLock?
Thank you :)
there is two way to hold the CPU running (on) even after screen lock
1. Wake Lock
wake lock hold cpu on as long as you acquire it. for acquiring wakelock
first define this permission in manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
then you can have wakelock like this
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
and after your task is done you can release wakelock like this
wakeLock.release();
UPDATE
WakefulBroadcastReceiver is deprecated now with android 8.0
2. WakefulBroadcastReceiver
it's a special broadcast receiver that takes care everything about wakelock
for using this first define in manifest
<receiver android:name=".WakefulReceiver"/>
and then same as we use our service you can do
public class WakefulReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
startWakefulService(context, your_service_intent);
}
}
here you can see all we have to do is start service using startWakefulService
this will take care everything

How to avoid user turn off the screen which result in the wifi turned off during downloading

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!

How to make my avd stay awake

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 );

Keep My App alive - Android

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.

How to modify Wi-Fi Sleep policy programmatically?

I want to keep Wi-Fi enabled when device goes to sleep mode, I tried several options, but nothing has worked out like acquire Wi-Fi lock and set wi-fi enabled. These options are not working then I tried with wake lock, this is working perfectly when my application running in the foreground, but when application is running in background, after some time excess wake lock error comes and application is getting destroyed and on top of that I can't use wake lock all the time because it dried out the battery.actual requirement is my application should run 24/7 and connectivity with the server always stays on because server can send data any time, but when device goes to sleep mode wi-fi is getting turned off so I need to set the wi-fi sleep policy to never upon starting of my application and will set back to normal policy when application gets destroyed. I tried following code in my main activity and runs the application and allow device to go to sleep mode and after some time connection is still getting closed:
Settings.System.putInt(getContentResolver(),
Settings.System.WIFI_SLEEP_POLICY,
Settings.System.WIFI_SLEEP_POLICY_NEVER);
So please do help me to resolve this issue.
Use import static android.provider.Settings.System.WIFI_SLEEP_POLICY; instead of the name string Settings.System.WIFI_SLEEP_POLICY.
It works perfectly.
In my case Settings.System.WIFI_SLEEP_POLICY does not work. I was able to keep wi-fi on with the PowerManager
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Then in the OnDestroy() I call
wl.release();

Categories

Resources