When the screen turns off, my application service is paused.
屏幕关闭时,我的应用程序服务暂停。
I start my service with the following code:
我使用以下代码启动我的服务:
if (mSharedPrefs.getBoolean("prefAutoUpdatesMain", false)) {
Intent svc = new Intent(this, MyService.class);
startService(svc);
}
How can I can avoid the service pause?
我怎样才能避免暂停服务?
What I have to do in MyService is to download some data from Internet. If I have understand the process I have to follow is:
我在MyService中要做的是从Internet下载一些数据。如果我理解了我必须遵循的过程:
- Acquire wakeLock
- 获取wakeLock
- Download data
- 下载数据
- Release wakeLock
- 发布wakeLock
In downloading data method there are no reference to wakeLock, it is the application to have the wakeLock, is it correct?
在下载数据方法时没有引用wakeLock,它是具有wakeLock的应用程序,是否正确?
Wake locks are reference counted by default. I think it is better a wakeLock without reference counting, to be sure to release it, am I wrong?
默认情况下,唤醒锁是引用计数。我认为没有引用计数更好的wakeLock,一定要释放它,我错了吗?
3 个解决方案
#1
64
A partial WakeLock is what you want. It will hold the CPU open, even if the screen is off.
部分WakeLock就是你想要的。即使屏幕关闭,它也会保持CPU打开。
To acquire:
获得:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
To release:
发布:
wakeLock.release();
WakeLock also supports reference counting so you may have multiple things in your service that require wake functionality, and the device can sleep when none of them are active.
WakeLock还支持引用计数,因此您的服务中可能有多个需要唤醒功能的东西,并且当它们都不活动时,设备可以休眠。
Things to watch out for:
需要注意的事项:
If you use reference counting, make sure all control paths through your application will properly acquire/release...finally blocks come in handy here.
如果您使用引用计数,请确保通过您的应用程序的所有控制路径将正确获取/释放...最后块在这里派上用场。
Also be sure to hold WakeLocks infrequently and for short periods of time. They add up in terms of battery use. Acquire your lock, do your business, and release as soon as possible.
还要确保不经常持有WakeLocks并持续很短的时间。他们在电池使用方面加起来。获取锁定,开展业务,并尽快发布。
#2
3
You need a partial wake lock.
你需要一个部分唤醒锁。
Detailed example here in a previous question:
上一个问题中的详细示例:
Wake locks android service recurring
唤醒锁定android服务重复出现
#3
1
I'm just using a foregrgound service.
我只是使用一个前卫的服务。
#1
64
A partial WakeLock is what you want. It will hold the CPU open, even if the screen is off.
部分WakeLock就是你想要的。即使屏幕关闭,它也会保持CPU打开。
To acquire:
获得:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
To release:
发布:
wakeLock.release();
WakeLock also supports reference counting so you may have multiple things in your service that require wake functionality, and the device can sleep when none of them are active.
WakeLock还支持引用计数,因此您的服务中可能有多个需要唤醒功能的东西,并且当它们都不活动时,设备可以休眠。
Things to watch out for:
需要注意的事项:
If you use reference counting, make sure all control paths through your application will properly acquire/release...finally blocks come in handy here.
如果您使用引用计数,请确保通过您的应用程序的所有控制路径将正确获取/释放...最后块在这里派上用场。
Also be sure to hold WakeLocks infrequently and for short periods of time. They add up in terms of battery use. Acquire your lock, do your business, and release as soon as possible.
还要确保不经常持有WakeLocks并持续很短的时间。他们在电池使用方面加起来。获取锁定,开展业务,并尽快发布。
#2
3
You need a partial wake lock.
你需要一个部分唤醒锁。
Detailed example here in a previous question:
上一个问题中的详细示例:
Wake locks android service recurring
唤醒锁定android服务重复出现
#3
1
I'm just using a foregrgound service.
我只是使用一个前卫的服务。