##Extend to Android 0.4
weex sdk support Module extend, Weex SDK provides only rendering capabilities, rather than have other capabilities, such as network, picture, and URL redirection. If you want the these features, you need to implement it.
For example: If you want to implement an address jumping function, you can achieve a Module Follow the steps below.
- Customize module must extend WXModule
- @WXModuleAnno annotation must be added, as it is the only the way to recognized by Weex
- The access levels of mehtod must be public
- The module class also can not be an inner class
- Customize can not be obfuscated by tools like ProGuard
- Module methods will be invoked in UI thread, do not put time consuming operation there
- Weex params can be int, double, float, String, Map, List
Refer to the following example:
public class WXEventModule extends WXModule{
private static final String WEEX_CATEGORY="com.taobao.android.intent.category.WEEX";
@WXModuleAnno
public void openURL(String url){
//implement your module logic here
}
}
you can add @JSMethod (uiThread = false or true )
annotation to choose the callback mode of moudle . see the follow example.
// as sync-callback mode
@JSMethod (uiThread = false)
public void testSyncCall(){
WXLogUtils.d("WXComponentSyncTest :"+ Thread.currentThread().getName());
}
// as async-callback mode
@JSMethod (uiThread = true)
public void testAsyncCall(){
WXLogUtils.e("WXComponentASynTest :"+ Thread.currentThread().getName() );
}
WXSDKEngine.registerModule("event", WXEventModule.class);
Now event
moudle is avaiable in weex, use the module like this:
var event = weex.requireModule('event');
event.openURL("http://www.github.com");
If the module need implement a callback to javascript, you just add JSCallback
argument to the method you want expose to javascript:
@WXModuleAnno
public void openURL(String url,JSCallback callback){
//implement your module logic here
Map<String,Object> resp = new HashMap();
resp.put("result","ok");
callback.invoke(resp);
}
At the javascript side, call the module with javascript function to receive callback data:
event.openURL("http://www.github.com",function(resp){ console.log(resp.result); });
- Customize components must extend WXComponent or WXContainer
- @WXComponentProp(name=value(value is attr or style of dsl)) for it be recognized by weex SDK.
- The access levels of mehtod must be public
- The component class can not be an inner class
- Customize can not be obfuscated by tools like ProGuard
- Component methods will be invoked in UI thread, do not put time consuming operation there.
- Weex params can be int, double, float, String, Map, List, Array
Refer to the following example
public class MyViewComponent extends WXComponent{
public MyViewComponent(WXSDKInstance instance, WXDomObject dom,
WXVContainer parent, String instanceId, boolean isLazy)
{
public MyViewComponent(WXSDKInstance instance, WXDomObject dom,
WXVContainer parent, String instanceId, boolean isLazy) {
super(instance, dom, parent, instanceId, isLazy);
}
@Override
protected void initView() {
mHost = new TextView(mContext);
}
@WXComponentProp(name=WXDomPropConstant.WX_ATTR_VALUE)
public void setMyViewValue(String value) {
((TextView)mHost).setText(value);
}
}
WXSDKEngine.registerComponent("MyView", MyViewComponent.class);
public class ImageAdapter implements IWXImgLoaderAdapter {
private Activity mContext;
public ImageAdapter(Activity activity) {
mContext = activity;
}
@Override
public void setImage(final String url, final ImageView view,
WXImageQuality quality, WXImageStrategy strategy) {
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
if (TextUtils.isEmpty(url)) {
view.setImageBitmap(null);
return;
}
String temp = url;
if (url.startsWith("//")){
temp = "http:" + url;
}
if (view.getLayoutParams().width<=0 || view.getLayoutParams().height<=0) {
return;
}
Picasso.with(WXEnvironment.getApplication())
.load(temp)
.resize(view.getLayoutParams().width,
view.getLayoutParams().height).into(view);
}
});
}
}