当应用程序在后台和屏幕锁定时,服务暂停

时间:2018-10-31 18:51:32

标签: android android-service

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

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

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

我尝试了commonsguy/cwac-wakeful

尝试过ignore battery optimization

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

服务

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上运行

1 个答案:

答案 0 :(得分:0)

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