Service gets paused while app is in background && screen is locked

淺唱寂寞╮ 提交于 2019-11-29 23:29:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!