gpt4 book ai didi

MainLooper 未执行 Android Runnable

转载 作者:行者123 更新时间:2023-11-29 23:13:48 25 4
gpt4 key购买 nike

应用简介:

  • 我有 Cordova/Ionic 应用程序和自定义 Cordova 插件以执行 native 代码。
  • 插件包含单独的 CameraActivity(扩展 FragmentActivity)以与 Camera 配合使用(部分代码基于 Camera2Basic 示例)。
  • 启动时,Activity 会显示 AnaliseFragment,其中应用程序会捕获每个相机帧并将图像传递给后台线程上的分析器。

执行步骤为:

  1. 用户按下 Cordova UI 上的按钮
  2. Cordova 通过cordova.exec(..) 执行原生插件方法
  3. native 插件通过 cordova.startActivityForResult(..) 为结果启动 CameraActivity
  4. CameraActivity 显示 AnaliseFragment
  5. AnaliseFragment 使用两个表面启动相机捕获 session :第一个显示在 TextureView 上,第二个由 ImageAnaliser 分析

问题:

Rarely and randomly UI stops reacting on user and runnables not executed on UI thread. At the same time background threads continue working as normal: camera output is visible on TextureView and ImageAnaliser continue receive images from Camera.

有没有人对如何查找/调试此类行为的原因有任何建议?或者有什么想法会导致这种情况?

我已经尝试过:

  • 记录 CameraActivity/AnaliseFragment 的每个生命周期事件 = 应用正常状态和 ANR 之间没有调用
  • 添加 WAKELOCK 以保持 Cordova MainActivity Activity = 没有帮助
  • 记录(跟踪)AnalilseFragment 和 ImageAnaliser 中的每个方法 = 没有可疑

这里是AnaliseFragment的简化代码:

public class AnaliseFragment extends Fragment {

private HandlerThread mBackgroundThread;
private Handler mBackgroundHandler;
private ImageAnalyser mImageAnalyser;

// listener is attached to camera capture session and receives every frame
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {

@Override
public void onImageAvailable(ImageReader reader) {
Image nextImage = reader.acquireLatestImage();
mBackgroundHandler.post(() ->
try {
mImageAnalyser.AnalizeNextImage(mImage);
}
finally {
mImage.close();
}
);
}
};

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
mImageAnalyser = new ImageAnalyser();
mImageAnalyser.onResultAvailable(boolResult -> {
// Runnable posted, but never executed
new Handler(Looper.getMainLooper()).post(() -> reportToActivityAndUpdateUI(boolResult));
});
}

@Override
public void onResume() {
super.onResume();
startBackgroundThread();
}

@Override
public void onPause() {
stopBackgroundThread();
super.onPause();
}

private void startBackgroundThread() {
if (mBackgroundThread == null) {
mBackgroundThread = new HandlerThread("MyBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
}

private void stopBackgroundThread() {
mBackgroundThread.quitSafely();
try {
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

ImageAnalyser 的简化代码:

public class ImageAnalyser  {

public interface ResultAvailableListener {
void onResult(bool boolResult);
}
private ResultAvailableListener mResultAvailableListener;
public void onResultAvailable(ResultAvailableListener listener) { mResultAvailableListener = listener; }

public void AnalizeNextImage(Image image) {
// Do heavy analysis and put result into theResult
mResultAvailableListener.onResult(theResult);
}
}

最佳答案

UI 线程中有一些长时间运行的操作。尝试 profile您的应用程序找出是什么阻塞了您的主线程。

关于MainLooper 未执行 Android Runnable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com