在本机实现中的runOnUiThread中没有触发代码。 runOnUiThread之前的代码会触发。我确信我做的不对。 我像这样创建了CodenameOne库
package com.uithread.test;
import com.codename1.system.NativeInterface;
public interface UIThreadNative extends NativeInterface {
public void runNativeCode();
}
package com.uithread.test;
import com.codename1.system.NativeLookup;
import com.codename1.ui.Dialog;
public class UIThreadManager {
private static UIThreadNative uithreadNative;
public UIThreadManager() {
if (uithreadNative == null) {
uithreadNative = (UIThreadNative) NativeLookup.create(UIThreadNative.class);
if (uithreadNative == null) {
Dialog.show("Null implementation", " UIThread is not implemented yet in this platform.", "Ok", null);
throw new RuntimeException("UIThread is not implemented yet in this platform.");
}
}
if (!uithreadNative.isSupported()) {
Dialog.show("Unsupported", " UIThread is not supported in this platform.", "Ok", null);
throw new RuntimeException("UIThread is not supported in this platform.");
}
}
public void runNativeCode() {
uithreadNative.runNativeCode();
}
}
android的原生实现
package com.uithread.test;
import android.content.Context;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.codename1.impl.android.*;
import com.codename1.ui.Dialog;
public class UIThreadNativeImpl {
private static Context context() {
return com.codename1.impl.android.AndroidNativeUtil.getActivity().getApplicationContext();
}
private static Activity activity() {
return com.codename1.impl.android.AndroidNativeUtil.getActivity();
}
public void runNativeCode() {
final Activity convenientActivity = activity();//AndroidNativeUtil.getActivity();
final CodenameOneActivity codenameoneActivity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
final android.app.Activity app = (Activity) AndroidNativeUtil.getActivity();
Dialog.show("Activity", convenientActivity + " convenientActivity", "Ok", null);
Dialog.show("Activity", codenameoneActivity + " codenameoneActivity", "Ok", null);
Dialog.show("Activity", app + " App", "Ok", null);
convenientActivity.runOnUiThread(new Runnable() {
public void run() {
Dialog.show("In run", "Run started", "Ok", null);
}
});
}
public boolean isSupported() {
return true;
}
}
在Statemachine中,我通过点击按钮在代码中运行它。
@Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
UIThreadManager uIThreadManager = new UIThreadManager();
uIThreadManager.runNativeCode();
}
正如我先前所说。 runOnUiThread之前的代码工作但runOnUiThread中的代码不起作用。本机实现中runNativeCode中的对话框用于检查不同风格的活动,这些活动正确显示不同的风格是相同的。
感谢。
答案 0 :(得分:0)
本机UI线程与我们的EDT完全不同,因此从该线程显示一个Codename One对话框将是一个巨大的EDT违规,可能会导致严重的崩溃!
由于我们的对话框安全地阻止,您将有效地阻止整个应用程序并使其崩溃。
我们使用的这行与你在Codename One和库中写的内容非常相同,例如here:
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { ... });