Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示器关闭时如何保持IntentService运行?_Java_Android_Android Intent_Intentservice - Fatal编程技术网

Java 显示器关闭时如何保持IntentService运行?

Java 显示器关闭时如何保持IntentService运行?,java,android,android-intent,intentservice,Java,Android,Android Intent,Intentservice,我正在使用android sdk 8开发一个简单的应用程序 在我的应用程序中,我实现了如下意图服务: public class NotificationService extends IntentService { public NotificationService() { super("NotificationService"); } @Override protected void onHandleIntent(Intent inten

我正在使用android sdk 8开发一个简单的应用程序

在我的应用程序中,我实现了如下意图服务:

public class NotificationService extends IntentService {

    public NotificationService() {
        super("NotificationService");
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        while (true) {
            /// Send http request to server
            /// notify if new messages received
            SystemClock.sleep(1000);
        }
    }

}
当显示屏打开时,该服务工作正常,但当手机休眠时,该服务将无法正常工作

我整天都在为这个问题苦苦挣扎。在一些教程中说,我们可以使用BroadcastReceiver和PowerManager保持cpu清醒,但我找不到解决方案

感谢您的帮助

使用PowerManager获取WakeLock以下示例使用字段PowerManager.WakeLock WakeLock

您可以通过以下方式释放WakeLock:

this.wakeLock.release();
例如,您可以在创建/启动或销毁/停止服务时调用这些方法

不要忘记将唤醒锁定权限添加到应用程序清单中

 <uses-permission android:name="android.permission.WAKE_LOCK" />

您必须防止手机睡眠-使用WAKELOCK
 <uses-permission android:name="android.permission.WAKE_LOCK" />