页面上调用原生代码,实现一个扫描功能,当后台扫描完成后,如何通知页面呢?目前我的做法是用的定时器,有什么其他的方法吗?后台应该怎么写才能调用js function?
zDoorService = plus.android.importClass("com.zhsq.kmh.DoorService");
zservice = new zDoorService();
zmain = plus.android.runtimeMainActivity();
zservice.scanDevice(zmain);
zInter = setInterval(function() {
if(zservice.getScaning() == false){
zserviceId = zservice.getDevs();
if(!zserviceId){
mui.alert("暂未匹配到设备,请重试");
}else{
mui.alert("匹配成功,设备id:"+zserviceId);
}
clearInterval(zInter);
zInter = null;
}
}, 100);
public class DoorService {
private static final String RET = "ret";
private static final int SEC = 3;
private ArrayList<String> devs;
private Boolean scaning = false;
public void scanDevice(Context context) {
scaning = true;
try {
ScanCallback callback = new ScanCallback() {
@Override
public void onScanResult(ArrayList<String> arrayList, ArrayList<Integer> arrayList1) {
devs = arrayList;
scaning = false;
}
@Override
public void onScanResultAtOnce(String s, int i) {
}
};
LibDevModel.scanDevice(context, false, SEC, callback);
} catch (Exception e) {
Log.e(e.getMessage(), e.getMessage());
}
}
public Boolean getScaning() {
return scaning;
}
}
1 个回复
Android_磊子
native层要回调js层,必须在native回调代码可以获得指定js代码所在webview的实例,你可以参照下sdk demo中扩展插件部分。