问题
Inside a Foreground Service I encode media with FFMPEG. By logging the encode progress I noticed the process gets paused(in some devices)
while device connected to adb over wifi(NOT USB) && screen is locked.
I tried :
try {
final PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (mgr != null) {
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG:WakeLock");
if (!wakeLock.isHeld()) {
wakeLock.acquire();
Log.i("AppController","Engaging WakeLock");
}else{
Log.i("AppController","WakeLock was already engaged");
}
}
} catch (Exception ignored) {
}
Tried AlarmManager inside Application class || My service class.
I tried commonsguy/cwac-wakeful.
Tried ignore battery optimization
Still CPU goes to sleep after one minute process.
Service
public class CompressionService extends Service {
private Messenger _activityMessenger;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
_activityMessenger = intent.getParcelableExtra(MESSENGER_INTENT_KEY);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IFFmpegProcessService.Stub mBinder = new IFFmpegProcessService.Stub() {
@Override
public void startEncode(final List<String> ffmpegArgs, final String outputFile) {
ExecuteBinaryResponseHandler handler = new ExecuteBinaryResponseHandler() {
@Override
public void onProgress(String s) {
long progress = FFmpegUtil.getProgres(s);
Log.d("JOB_PROGRESS: " + progress);
sendMessage(progress);
}
};
FFmpegUtil.call(handler);
}
@Override public void cancel() throws RemoteException { }
@Override public boolean isEncoding() throws RemoteException {return false; }
};
private void sendMessage(@Nullable Object params) {
Message m = Message.obtain();
m.what = MessageId.JOB_PROGRESS.ordinal();
m.obj = params;
try {
_activityMessenger.send(m);
} catch (RemoteException e) { e.printStackTrace();}
}
}
Running on API 24 || API 26 Honor 8 Lite
回答1:
You need to implement it as ForegroundService. It is required to avoid the pausing at least from API Level 26.
来源:https://stackoverflow.com/questions/53090171/service-gets-paused-while-app-is-in-background-screen-is-locked