weex android 扫描二维码

weex android 其实已经内置了zxing的扫描二维码的功能,我们需要做的就是将其封装成一个weex可以使用的module即可

step1 新建 ScanModule.java

public class ScanModule extends WXModule {


    @JSMethod
    public void scanCode(JSCallback callback) {

        WXPageActivity act=(WXPageActivity)mWXSDKInstance.getContext();
        act.setOnScanFinishCallback(callback);

        IntentIntegrator integrator = new IntentIntegrator(act);
        integrator.initiateScan();
    }
}

step2 WXPageActivity 中做些修改

//声明一个 jscallback 来接收扫描的结果
private JSCallback onScanFinishCallback=null;
//新增一个set方法
  public void setOnScanFinishCallback(JSCallback callback){
    this.onScanFinishCallback=callback;
  }
//修改下面方法,添加自定义处理
  private void handleDecodeInternally(String code) {

    if (!TextUtils.isEmpty(code)) {
      Uri uri = Uri.parse(code);
      if (uri.getQueryParameterNames().contains("bundle")) {
        WXEnvironment.sDynamicMode = uri.getBooleanQueryParameter("debug", false);
        WXEnvironment.sDynamicUrl = uri.getQueryParameter("bundle");
        String tip = WXEnvironment.sDynamicMode ? "Has switched to Dynamic Mode" : "Has switched to Normal Mode";
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
        finish();
        return;
      } else if (uri.getQueryParameterNames().contains("_wx_devtool")) {
        WXEnvironment.sRemoteDebugProxyUrl = uri.getQueryParameter("_wx_devtool");
        WXEnvironment.sDebugServerConnectable = true;
        WXSDKEngine.reload();
        Toast.makeText(this, "devtool", Toast.LENGTH_SHORT).show();
        return;
      } else if (code.contains("_wx_debug")) {
        uri = Uri.parse(code);
        String debug_url = uri.getQueryParameter("_wx_debug");
        WXSDKEngine.switchDebugModel(true, debug_url);
        finish();
      } else {
       //新增自定义处理代码
        if(this.onScanFinishCallback!=null){   
          Map data = new HashMap();
          data.put("result",true);
          data.put("data",code);
          this.onScanFinishCallback.invokeAndKeepAlive(data);
        }else{
          Toast.makeText(this, code, Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(Constants.ACTION_OPEN_URL);
          intent.setPackage(getPackageName());
          intent.setData(Uri.parse(code));
          startActivity(intent);
        }

      }
    }
  }

step3 注册module

在WXApplication.java 的 onCreate方法中添加

WXSDKEngine.registerModule("scan", ScanModule.class);

step4 在*.vue文件中使用自定义module

const scan = weex.requireModule('jd-scan')

scan.scanCode(res=>{
                                //res 即为返回的数据
                                //自定义处理
})

你可能感兴趣的:(weex android 扫描二维码)