2015-12-03 49 views
2

没有代码在本机实现中的runOnUiThread中触发。 runOnUiThread之前的代码会触发。我相信我没有做正确的事情。 我创建了CodenameOne图书馆这样CN1库 - runOnUiThread中没有代码触发

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

原生用户界面线程与我们的EDT完全不同,因此从该线程显示的Codename One对话框将是一个巨大的EDT违例,可能导致严重的崩溃!

由于我们的对话框安全地阻止了,您将有效地阻止整个应用程序并使其崩溃。

我们使用这条线,它非常类似于您在Codename One和库中写的很多内容,例如here

AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { ... }); 
+0

非常感谢。保存了我的一天。所有的android原生代码完美射击。 –