当应用程序处于后台&&中时,服务会暂停屏幕被锁定 [英] Service gets paused while app is in background && screen is locked

查看:125
本文介绍了当应用程序处于后台&&中时,服务会暂停屏幕被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在前景服务中,我使用 FFMPEG 对媒体进行编码.通过记录编码进度,我注意到该过程已暂停(在某些设备中)

Inside a Foreground Service I encode media with FFMPEG. By logging the encode progress I noticed the process gets paused(in some devices)

device connected to adb over wifi(NOT USB) && screen is locked.

我尝试过:

    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) {
    }

Application类中尝试了AlarmManager || My service类.

Tried AlarmManager inside Application class || My service class.

我尝试了 commonsguy/cwac-wakeful .

尝试了ignore battery optimization

经过一分钟的处理后,仍然CPU进入睡眠状态.

Still CPU goes to sleep after one minute process.

服务

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();}
}


}

API 24 || API 26 Honor 8 Lite

推荐答案

您需要将其实现为ForegroundService.必须至少从API级别26避免暂停.

You need to implement it as ForegroundService. It is required to avoid the pausing at least from API Level 26.

这篇关于当应用程序处于后台&amp;&amp;中时,服务会暂停屏幕被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆