如何避免用户关闭而导致在WiFi下载过程中关闭屏幕 [英] How to avoid user turn off the screen which result in the wifi turned off during downloading

查看:90
本文介绍了如何避免用户关闭而导致在WiFi下载过程中关闭屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用的AsyncTask 下载较大文件(300M +)。我注意到,当用户关闭其屏幕(锁定他们的设备),在WiFi将断开,下载将挂起。

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?

推荐答案

您需要在您的应用程序来实现激活锁定。激活锁定将唤醒CPU柜面的屏幕是关闭的,在正常的方式进行操作。

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.

写下以下code开始前的AsyncTask

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

您需要编写 wl.release(); 上PostExecution()方法。而你需要在AndroidManifest.xml中定义权限,如下所示,

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" />

这篇关于如何避免用户关闭而导致在WiFi下载过程中关闭屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆