Weex SDK provides only rendering capability, rather than having other capabilities, such as network, picture, and URL redirection. If you want the these features, you need to implement them yourselves.
The example below will describe how to extend weex with native logic or 'bridge' your existed native code.
WXModule
in native:
public class URLHelperModule extends WXModule{ private static final String WEEX_CATEGORY="com.taobao.android.intent.category.WEEX"; @WXModuleAnno public void openURL(String url){ if (TextUtils.isEmpty(url)) { return; } StringBuilder builder=new StringBuilder("http:"); builder.append(url); Uri uri = Uri.parse(builder.toString()); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.addCategory(WEEX_CATEGORY); mWXSDKInstance.getContext().startActivity(intent); } }
Notice the @WXModuleAnno
, use this annotation to mark the methods you wanna expose to javascript.
If your also want to callback to javascript, you should define a callbackId
parameter to received 'JS callback function id':
public class URLHelperModule extends WXModule{ @WXModuleAnno public void openURL(String url,String callbackId){ //... //callback to javascript Map<String, Object> result = new HashMap<String, Object>(); result.put("ts", System.currentTimeMillis()); WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, result); } }
try {
WXSDKEngine.registerModule("myURL", URLHelperModule.class);//'myURL' is the name you'll use in javascript
} catch (WXException e) {
WXLogUtils.e(e.getMessage());
}
let URLHelper = require('@weex-module/myURL');//same as you registered URLHelper.openURL("http://www.taobao.com",function(ts){ console.log("url is open at "+ts); });
WXModule
;