How to keep Android app background service running when the screen is locked

In the development of the app, do not allow the music player to keep the music from playing even when the screen is locked. This requires
keeping the cpu awake during sleep to run the service, and the api in PowerManager.WakeLock can achieve this. Function

There are four levels of wake lock:

insert image description here

So if you want to keep the cpu awake, use PARTIAL_WAKE_LOCK

custom service

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

Guess you like

Origin blog.csdn.net/baidu_41666295/article/details/123369341