我正在使用从Codenameone调用的Native Android Code。我正在发现蓝牙设备,连接到它们等等。但是我已经使用局部变量(如bluetoothDevices)来设置StateMachine调用的这些活动的返回值。代码工作正常,但我想用回调方法替换它们。我该怎么做?
执行发现的PrinterNativeCallsImpl.java等。
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);
}
}
}
});
}
}
使用它的StateMachine的示例代码:
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()类型的调用?
答案 0 :(得分:1)
我建议在Codename One src目录中添加一个静态类,只调用该类的静态方法。这对于Android版本来说非常简单。
E.g。在您的Codename One 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调用约定来调用这样的静态方法,但它应该很容易实现。