简体   繁体   English

如何在屏幕关闭时保持Android服务的运行?

[英]How can I keep my Android service running when the screen is turned off?

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. 我在MyService中要做的是从Internet下载一些数据。 If I have understand the process I have to follow is: 如果我理解了我必须遵循的过程:

  1. Acquire wakeLock 获取wakeLock
  2. Download data 下载数据
  3. 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更好,一定要释放它,我错了吗?

A partial WakeLock is what you want. 部分WakeLock就是你想要的。 It will hold the CPU open, even if the screen is off. 即使屏幕关闭,它也会保持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. 还要确保不经常使用WakeLocks并持续很短的时间。 They add up in terms of battery use. 他们在电池使用方面加起来。 Acquire your lock, do your business, and release as soon as possible. 获取锁定,开展业务,并尽快发布。

You need a partial wake lock. 你需要一个部分唤醒锁。

Detailed example here in a previous question: 上一个问题中的详细示例:

Wake locks android service recurring 唤醒锁定android服务重复出现

我只是在使用一种前卫的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中关闭屏幕时如何防止CPU“休眠”? - How to keep CPU from 'sleeping' when screen is turned off in Android? 当屏幕关闭或锁定在 Java 的 Android 工作室中时,如何继续运行活动? - How can I keep running an activity when screen is off or locked in Android studio in Java? 设备处于锁定状态且屏幕处于关闭状态时,如何运行我的应用程序? - How can I run my application when device is in locked state and screen is in turned off state? 即使在Android中关闭了屏幕,如何保持应用状态? - How to keep app status even if screen is turned off in Android? 屏幕关闭时,服务中的android加速计停止运行 - android accelerometer in service stops when screen is turned off Android服务在屏幕关闭时停止收集数据 - Android service stops collecting data when the screen is turned off 锁定萤幕时如何保持我的应用程式在Android上运作? - How to keep my app running on Android when I lock the screen? 屏幕关闭时继续接收数据包 - keep receiving packet when the screen is turned off 屏幕关闭并按下主页时如何保持Android服务活跃? - How to keep Android service alive when screen off and home pressed? 屏幕关闭时如何保持应用程序运行(最新版Android) - How to keep an app running when screen goes off(latest android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM