在开发人员控制台 - Android Vitals 中,我看到应用程序有“卡住部分唤醒锁”的警告。在文档中它被描述为
如果在您的应用程序在后台运行时长时间保持部分唤醒锁定,则会卡住(用户看不到应用程序的任何部分)。
确实,应用程序在后台运行(播放音频),但我使用前台服务,并且应用程序持有锁定通知的整个时间都是可见的。
稍后在文档中您可以阅读
确保您的应用程序的某些部分保留在前台。例如,如果您需要运行服务,请改为启动前台服务。这直观地向用户表明您的应用程序仍在运行。
但这似乎不是真的。Android Vitals 是否忽略了该应用具有前台服务?有没有人有类似的经历?
编辑:这是来自我的 AudioService 的代码
PowerManager.WakeLock mWakeLock = null;
boolean mHasWakeLock;
void acquireLocks(){
if (!mHasWakeLock) {
final PowerManager pm = (PowerManager)
context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,TAG);
mWakeLock.setReferenceCounted(false);
mWakeLock.acquire();
mHasWakeLock = true;
}
}
void releaseLocks() {
if (mWakeLock != null) {
mWakeLock.release();
mWakeLock = null;
}
mHasWakeLock = false;
}
void play(){
startForeground(NOTIFICATION_ID, notification);
acquireLocks();
}
void stop(){
releaseLocks();
stopForeground(true);
}