在前台服务中我使用FFMPEG对媒体进行编码.通过记录编码进度,我注意到进程暂停(在某些设备中)
设备通过wifi连接到adb(非USB)&&屏幕被锁定.
我试过了 :
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我的服务类.
试图忽略电池优化
一分钟后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 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