2015-12-03 38 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原生代碼完美射擊。 –