weex☞获取通讯录

获取通讯录、扫码、打开相册、拍照等操作,都是一个套路,就是创建module,原生实现这些方法,前端调用是时候返回最终的结果。
下面是如何获取通信录代码,不知道有没有更好的策略。

public class pickPhone extends WXModule {
    String stringBuffer = "";
    protected JSCallback callback;

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

        this.callback = callback;
    }

    @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);
            }

            Log.d("my-tag", stringBuffer);

            Map params = new HashMap<>();
            params.put("data", stringBuffer);
            callback.invoke(stringBuffer);
        }
    }
}

配置相应的权限,在WXApplication中加入

   WXSDKEngine.registerModule("pickPhone", pickPhone.class);

然后就是前端调用

const pickPhone = weex.requireModule('pickPhone')
...         //在方法中直接使用就行了
            pickPhone .open(res => {
                   ...
                })

你可能感兴趣的:(weex☞获取通讯录)