2013-11-27 18 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目錄中添加一個靜態類,然後調用該類的靜態方法。這對於Android版本來說非常簡單。

E.g.在你的代號一個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(...); 

對於iOS版本則需要使用多少有些無證XMLVM調用約定調用這樣的靜態方法,但它應該很容易實現。