Skip to content Skip to sidebar Skip to footer

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 downl

Solution 1:

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,

PowerManagerpm= (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLockwl= 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-permissionandroid:name="android.permission.WAKE_LOCK" />

Solution 2:

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!

Post a Comment for "How To Avoid User Turn Off The Screen Which Result In The Wifi Turned Off During Downloading"