我正在开发一个在后台运行的移动应用程序。它会每隔一段时间在后台截取屏幕截图,但是一段时间后,ActivityManager 会使用日志终止进程,I/ActivityManager: killing com.example: excessive cpu xxxx during yyyyy dur=zzzzz limit=2
我能做些什么来防止ActivityManager
杀死应用程序?
我使用 LeakCanary 检查内存泄漏,但发现 0 泄漏
我试过:
- minifyEnabled true
- shrinkResources true
- proguardFiles
我正在使用 a每隔一段时间Handler
自动(主要是MediaProjection
和)运行我的程序。ImageReader
在遵循其他问题和谷歌的教程后,我制作了Handler
静态文件,但同样的问题仍然存在。
MainActivity.java
:
public void startCapturing() {
try {
mHandler.post(captureInterval);
Toast.makeText(this, "started", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private static class SafeRunnable implements Runnable {
private final WeakReference<MainActivity> mReference;
SafeRunnable(MainActivity reference) {
mReference = new WeakReference<>(reference);
}
@Override
public void run() {
final MainActivity reference = mReference.get();
if (reference != null) {
runWithParent(reference);
}
}
public void runWithParent(MainActivity reference) {
}
}
private final Runnable captureInterval = new SafeRunnable(this) {
@Override
public void runWithParent(MainActivity reference) {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
destroyImageReader();
destroyVirtualDisplay();
if (!mKeyguardManager.isKeyguardLocked()) {
if (mMediaProjection == null) {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
} else {
createVirtualDisplay();
}
}
reference.mHandler.removeCallbacksAndMessages(captureInterval);
reference.mHandler.postDelayed(this, 5000);
}
};