2013-11-27 2 views
2

Codenameone에서 호출 한 기본 Android 코드를 사용하고 있습니다. 블루투스 장치를 발견하고 연결하는 등의 작업을 수행합니다. 그러나 bluetoothDevices와 같은 로컬 변수를 사용하여 StateMachine에서 호출 한 이러한 활동에서 반환 값을 설정했습니다. 코드는 제대로 작동하지만 콜백 메서드로 대체하고 싶습니다. 어떻게해야합니까?콜백을 사용하여 codenameone의 로컬 변수를 대체하십시오.

검색 등을 수행하는 PrinterNativeCallsImpl.java. 머신 (StateMachine)에서

public class PrinterNativeCallsImpl { 
//#ifndef REMOVE_DEBUG 
    private static final Logger logger = LoggerFactory.getLogger(PrinterNativeCallsImpl.class); 
//#endif 
    // ???? WANT TO AVOID DOING THIS 
    public String bluetoothDevices = ""; 
    public String selprinter = ""; 
    public String errorMsg = ""; 
    private int result = 9; 
    public String printerInfo = ""; 

    public void connect(String printer){ 

     final Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity(); 
     final Intent connectIntent = new Intent(ctx, BluetoothFilePrinter.class); 

     result = PrinterNativeCalls.PENDING; 

     Bundle bundle = new Bundle(); 
     bundle.putString(BTWrapperActivity.EXTRA_DEVICE_ADDRESS, printer); 
     bundle.putBoolean("IS_CONNECTED", false); 
     bundle.putInt(BluetoothFilePrinter.REQUEST_CODE, BluetoothFilePrinter.CONNECT_REQUEST); 
     connectIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     connectIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
     connectIntent.putExtras(bundle); 
     startActivityForResult(connectIntent, BluetoothFilePrinter.CONNECT_REQUEST, new IntentResultListener(){ 

      public void onActivityResult(int requestCode, int resultCode, Intent data) { 
       if (data == null) { 
        data = connectIntent; 
       } 
       if (requestCode == BluetoothFilePrinter.CONNECT_REQUEST) { 
        if (resultCode == Activity.RESULT_OK) { 
         selprinter = data.getExtras().getString(BTWrapperActivity.EXTRA_DEVICE_ADDRESS); 
         result = PrinterNativeCalls.OK; 
        } else { 
         result = PrinterNativeCalls.ERROR; 
         errorMsg = data.getExtras().getInt(BluetoothFilePrinter.ERROR) 
           +" - "+data.getExtras().getString(BluetoothFilePrinter.ERROR_MSG); 
        } 
       } 
      } 
     }); 
    } 

    /** 
    * Start an intent for result 
    */ 
    public static void startActivityForResult(Intent intent, int actRequestCode, final IntentResultListener listener){ 
     final com.codename1.impl.android.CodenameOneActivity act = (com.codename1.impl.android.CodenameOneActivity) com.codename1.impl.android.AndroidNativeUtil.getActivity(); 
     act.startActivityForResult(intent, actRequestCode); 
     act.setIntentResultListener(new IntentResultListener() { 

      @Override 
      public void onActivityResult(int requestCode, int resultCode, Intent data) { 
       listener.onActivityResult(requestCode, resultCode, data); 
       act.restoreIntentResultListener(); 
      } 
     }); 
    } 


    public int getResult() { 
     return result; 
    } 

    public String getSelprinter() { 
     return selprinter; 
    } 

    public String getErrorMsg(){ 
     return errorMsg; 
    } 

    public boolean isSupported() { 
     return true; 
    } 

    public String getPrinterStatus() { 
     return printerInfo; 
    } 

    public String getBluetoothDevices() { 
     return bluetoothDevices; 
    } 

    public void discoverDevices(){ 
     //showDlg(); 
     final Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity(); 
     final Intent discIntent = new Intent(ctx, BluetoothFilePrinter.class); 

     result = PrinterNativeCalls.PENDING; 

     Bundle bundle = new Bundle(); 
     bundle.putInt(BluetoothFilePrinter.REQUEST_CODE, BluetoothFilePrinter.DISCOVER_REQUEST); 
     discIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     discIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
     discIntent.putExtras(bundle); 
     startActivityForResult(discIntent, BluetoothFilePrinter.DISCOVER_REQUEST, new IntentResultListener(){ 

      public void onActivityResult(int requestCode, int resultCode, Intent data) { 
       if (data == null) { 
        data = discIntent; 
       } 
       if (requestCode == BluetoothFilePrinter.DISCOVER_REQUEST) { 
        if (resultCode == Activity.RESULT_OK) { 
         bluetoothDevices = data.getExtras().getString(BTWrapperActivity.DEVICES_DISCOVERED); 
         result = PrinterNativeCalls.OK; 
        } else { 
         result = PrinterNativeCalls.ERROR; 
         errorMsg = data.getExtras().getInt(BluetoothFilePrinter.ERROR) 
           +" - "+data.getExtras().getString(BluetoothFilePrinter.ERROR_MSG); 
        } 
       } 
      } 
     }); 

    } 


} 

샘플 코드를 사용하는 :

private void discoverBTDevices(){ 

    final PrinterNativeCalls mPrinter = (PrinterNativeCalls) NativeLookup.create(PrinterNativeCalls.class); 
    isPrinterMode = true; 
    final Dialog okDialog = (Dialog) createContainer(fetchResourceFile(), "OkDialog"); 

    new Thread() { 
     public void run() { 

      mPrinter.discoverDevices(); 
      while (mPrinter.getResult() == PrinterNativeCalls.PENDING) { 
       try { 
        sleep(100); 
       } catch (InterruptedException ex) { 
        // Ignore interrupted exception 
       } 
      } 
      int result = mPrinter.getResult(); 

      if (result == PrinterNativeCalls.ERROR) { 
       String errMsg = mPrinter.getErrorMsg(); 
       if (errMsg!=null && errMsg.length()>=3) { 
        addGreenRedTitle(okDialog, false, errMsg); 
       }else{ 
        addGreenRedTitle(okDialog, false, "DISCOVERY did not complete due to an error. Please retry"); 
       } 
       isConnected = false; 
      } else if (result == PrinterNativeCalls.PENDING) { 
       //Do nothing 
      } else if (result == PrinterNativeCalls.OK) { 
       final String devices = mPrinter.getBluetoothDevices(); 
       devicesVect = getAllDevices(devices); 
       showFormInEDT("currency", null); 

      } 
     } // eof run 
    }.start(); 


} 

가 어떻게) (통화의 종류를 mPrinter.getBluetoothDevices을 방지하기 위해 콜백을 사용합니까?

답변

1

Codename One src 디렉토리에 정적 클래스를 추가하고 해당 클래스에서 정적 메소드를 호출하는 것이 좋습니다. 이것은 안드로이드 버전 꽤 사소한입니다.

예. 당신의 코드 네임 하나 src 디렉토리에서 (오른쪽 패키지에서) :

public class BTCallbacks { 
    public static void deviceDiscovered(String name) { 
     // handle this in code, keep in mind its the Android thread so make sure to use callSerially()! 
    } 
} 

그리고 네이티브 코드에서 수행

BTCallbacks.deviceDiscovered(...); 

을 당신이 호출 규칙을 다소 문서화되지 않은 XMLVM를 사용해야합니다 아이폰 OS 버전 정적 메소드를 호출하지만 쉽게 수행 할 수 있어야합니다.

관련 문제