weex和Android的交互

项目需求用weex开发,所以写下希望对需要的人有所帮助吧

weex调用native的方法时


    @JSMethod(uiThread = true)
    public void printLog(String msg) {
        WXLogUtils.d("打印日志",msg);
    }

native传递参数给weex时

    @JSMethod(uiThread = false)
    public void toast(JSCallback callback, String msg) {
        Map map = new HashMap();
        map.put("data", msg);
        callback.invoke(map);
    }

native主动调用weex的方法现在还没有但是可以使用event解决:

   Map params=new HashMap<>();
            params.put("data",stringBuffer);
            mWXSDKInstance.fireGlobalEventCallback("phoneBookLocation",params);

我这边做了一个调用原生通讯录传个weex的Module:

public class PhoneBookModule extends WXModule {
    String stringBuffer = "";

    @JSMethod(uiThread = false)
    public void open() {
        ((Activity) (mWXSDKInstance.getContext())).startActivityForResult(new Intent(Intent.ACTION_PICK,
                ContactsContract.Contacts.CONTENT_URI),0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0){

            ContentResolver reContentResolverol = mWXSDKInstance.getContext()
                    .getContentResolver();
            if(data==null){
                return;
            }
            Uri contactData = data.getData();
            @SuppressWarnings("deprecation")
            Cursor cursor = ((Activity)(mWXSDKInstance.getContext())).managedQuery(contactData, null, null,
                    null, null);
            if (cursor == null) {
                return;
            }
            cursor.moveToFirst();
            if (cursor.getCount() == 0) {
                Toast.makeText(mWXSDKInstance.getContext(),"您拒绝了访问通讯录,请设置访问权限!",Toast.LENGTH_SHORT).show();
                return;
            }
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = reContentResolverol.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                            + contactId, null, null);
            ArrayList contactList = new ArrayList();
            while (phone.moveToNext()) {
                stringBuffer = phone
                        .getString(phone
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                stringBuffer = stringBuffer.replace("+86", "");
                stringBuffer = stringBuffer.replace(" ", "");
                stringBuffer = stringBuffer.replace("-", "");
                stringBuffer = stringBuffer.replace("——", "");
                contactList.add(stringBuffer);
            }
            if (null == contactList || 0 == contactList.size()) {
                Toast.makeText(mWXSDKInstance.getContext(), "该联系人没有可用手机号码!",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            if (contactList.size() > 1) {
                final String[] contactArr = new String[contactList.size()];
                for (int i = 0; i < contactArr.length; i++) {
                    contactArr[i] = contactList.get(i);
                }
                new AlertDialog.Builder(mWXSDKInstance.getContext())
                        .setTitle("请选择充值号码")
                        .setSingleChoiceItems(contactArr, 0,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        // TODO Auto-generated
                                        // method stub
                                        stringBuffer = contactArr[which];
                                        dialog.dismiss();
                                    }
                                }).show();
            } else {
                stringBuffer = contactList.get(0);
            }
            Map params=new HashMap<>();
            params.put("data",stringBuffer);
            mWXSDKInstance.fireGlobalEventCallback("phoneBookLocation",params);
        }
    }
}

weex调用native方法:

weex监听事件

var globalEvent = require('@weex-module/globalEvent');
globalEvent.addEventListener("phoneBookLocation", function (e) {
  console.log("get geolocation")
});


你可能感兴趣的:(android)