gpt4 book ai didi

android - 使用Android,如何将控制权传递回主线程?

转载 作者:行者123 更新时间:2023-12-02 17:52:57 24 4
gpt4 key购买 nike

我使用OpenCV教程3代码(摄像机控制)(请参阅OpenCV4Android samples)作为我的代码的基础,并且一旦将图像保存在onPictureTaken()方法中,就需要能够打开和操作该图像。在操作期间,我希望能够显示图像的更改并使用Toast发布消息,这两者都要求我位于主线程中。

根据迄今为止收到的建议,我正在尝试在BroadcastReceiver中使用Activity,这是由sendBroadcast()中的JavaCameraView触发的。

我已经将Activity更新为包括BroadcastReceiver,并按如下所示进行注册和注销:

public class Tutorial3Activity extends Activity implements CvCameraViewListener2, OnTouchListener {
:
private ActivityReceiver activityReceiver;
:
@Override
public void onCreate(Bundle savedInstanceState) {
:
IntentFilter intentFilter = new IntentFilter();
this.registerReceiver(activityReceiver, intentFilter);
:
}
:
public void onDestroy() {
:
this.unregisterReceiver(activityReceiver);
:
}
:
private class ActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BroadcastReceiver triggered");
if (intent.getAction().equals(Tutorial3View.IMAGE_READY)) {
Log.i(TAG, "BroadcastReceiver received");
}
}
}
}
sendBroadcast()JavaCameraView中被调用,如下所示:
public class Tutorial3View extends JavaCameraView {
:
public static final String IMAGE_READY = "IMAGE_READY";
private Context context;
:
public Tutorial3View(Context context, AttributeSet attrs) {
:
this.context = context;
:
}
:
public void takePicture(final String fileName) {
PictureCallback callback = new PictureCallback() {
private String mPictureFileName = fileName;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
:
image_read();
:
}
};
mCamera.takePicture(null, null, callback);
}
:
public void image_read() {
Log.i(TAG, "BroadcastReceiver - send");
Intent i = new Intent();
i.setAction(IMAGE_READY);
context.sendBroadcast(i);
Log.i(TAG, "BroadcastReceiver - sent");
}
}
Manifest包含 BroadcastReceiver,如下所示:
<receiver android:name="ActivityReceiver">
<intent-filter android:label="IMAGE_READY"></intent-filter>
</receiver>

当我拍照时,LogCat会显示:
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - send
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - sent

但没有 BroadcastReceiver triggeredBroadcastReceiver received的日志。

有人可以建议为什么不接收广播吗?

谢谢

最佳答案

您需要调用 runOnUiThread() 方法:

runOnUiThread(new Runnable() {
@Override
public void run() {
// stuff to be done on UI Thread
}
});

关于android - 使用Android,如何将控制权传递回主线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15718145/

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