CN1 库 - runOnUiThread 中没有代码触发
CN1 Library - No code fires in runOnUiThread
在本机实现的 运行OnUiThread 中没有代码触发。 运行OnUiThread 之前的代码会触发。我确定我做的事情不对。
我像这样创建了 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 I 中 运行 在代码中单击按钮。
@Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
UIThreadManager uIThreadManager = new UIThreadManager();
uIThreadManager.runNativeCode();
}
正如我之前所说。 运行OnUiThread 之前的代码有效,但 运行OnUiThread 中的代码无效。本机实现中 运行NativeCode 中的对话框用于检查不同风格的 activity,它正确显示不同风格是相同的。
谢谢。
原生 UI 线程与我们的 EDT 完全不同,因此从该线程显示代号一对话框将严重违反 EDT,可能导致严重崩溃!
由于我们的对话框安全阻塞,您实际上会阻塞整个应用程序并使其崩溃。
我们使用这一行,这与你在代号一和库中写的相当多,例如here:
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { ... });
在本机实现的 运行OnUiThread 中没有代码触发。 运行OnUiThread 之前的代码会触发。我确定我做的事情不对。 我像这样创建了 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 I 中 运行 在代码中单击按钮。
@Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
UIThreadManager uIThreadManager = new UIThreadManager();
uIThreadManager.runNativeCode();
}
正如我之前所说。 运行OnUiThread 之前的代码有效,但 运行OnUiThread 中的代码无效。本机实现中 运行NativeCode 中的对话框用于检查不同风格的 activity,它正确显示不同风格是相同的。
谢谢。
原生 UI 线程与我们的 EDT 完全不同,因此从该线程显示代号一对话框将严重违反 EDT,可能导致严重崩溃!
由于我们的对话框安全阻塞,您实际上会阻塞整个应用程序并使其崩溃。
我们使用这一行,这与你在代号一和库中写的相当多,例如here:
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { ... });