Android的服务停止工作时,手机被锁定 [英] Android service stops working when phone locked

查看:148
本文介绍了Android的服务停止工作时,手机被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有启动的服务的应用程序。该服务具有定时器和弹出通知每30秒。我注意到以下行为:

I have an application which starts a service. The service has a timer and pops a notification every 30 seconds. I notice the following behavior:

  • 如果屏幕上的通知显示正常(即使该应用程序在后台)
  • 如果屏幕关闭(按电源按钮或本身)的通知,将不显示
  • 如果屏幕是关闭的,但我有运行的通知出现在调试

我怎样才能使与屏幕关闭该服务的运行。

How can I make the service run with the screen off.

(实际的应用程序只检查是否通知应每隔30秒进行检查,但是出于测试目的,出现上述情况是好的)

(The actual app only checks if a notification should be checked every 30 seconds, but for testing purposes, the above scenario is ok)

谢谢!

推荐答案

我有一些问题。

您可以使用WakeLock做的工作! 事情是这样的:

You may use the WakeLock to do the work! Something like this:

PowerManager pm = (PowerManager)getApplicationContext().getSystemService(
          getApplicationContext().POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();

然后,使用这样的:

Then use this:

wl.release();

释放wakelock。

to release the wakelock.

不要伪造补充:

<使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>

到清单!

这篇关于Android的服务停止工作时,手机被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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