闪闪
闪闪
  • 发布:2015-01-26 21:47
  • 更新:2015-01-26 21:47
  • 阅读:29991

调用系统通讯录选择手机号 Android插件

分类:5+ SDK

plus.contacts加载全部联系人,只为选择一个联系人时,联系人较多情况下,速度很慢。搞了个插件来调用系统通讯录,只返回选择的一个联系人。

使用:

plus.nativecontact.pick(function(contact){//成功  
           alert(contact.numbers[0].value);//所选择联系人的第一个电话号码  
    }, function(status){//失败  

    });  
//contact为联系人json对象,形式:{"givenName":"张三","numbers":[{"value":"123"},{"value":"456"}]}

添加权限:

"nativecontact":{  
            "description": "系统通讯录"  
        }

plugin.js:

document.addEventListener("plusready",  function()  
{  
    var B = window.plus.bridge;  
    var nativecontact =   
    {  
        "pick":function(successCallback, errorCallback){  
            var success = typeof successCallback !== 'function' ? null : function(args) {  
                        successCallback(args);  
                    },  
                fail = typeof errorCallback !== 'function' ? null : function(code) {  
                        errorCallback(code);  
                    },  
                callbackID = B.callbackId(success, fail);  
            return B.exec("nativecontact", "pick", [callbackID]);  
        }  
    };  
    window.plus.nativecontact = nativecontact;  
}, true);

NativeContact.java:

import android.app.Activity;  
import android.content.Context;  
import android.content.Intent;  
import android.database.Cursor;  
import android.net.Uri;  
import android.provider.ContactsContract;  
import io.dcloud.DHInterface.AbsMgr;  
import io.dcloud.DHInterface.IApp;  
import io.dcloud.DHInterface.IFeature;  
import io.dcloud.DHInterface.ISysEventListener;  
import io.dcloud.DHInterface.ISysEventListener.SysEventType;  
import io.dcloud.DHInterface.IWebview;  
import io.dcloud.util.JSONUtil;  
import io.dcloud.util.JSUtil;  

public class NativeContact implements IFeature{  
    private final static int REQUESTCODE = 1;  

    @Override  
    public void dispose(String arg0) {  
        // TODO Auto-generated method stub  

    }  

    @Override  
    public String execute(final IWebview pWebview, final String action, final String[] pArgs) {  
        if("pick".equals(action))  
        {  
            final IApp _app = pWebview.obtainFrameView().obtainApp();  
            final String callBackId = pArgs[0];  
            _app.registerSysEventListener(new ISysEventListener(){  
                @Override  
                public boolean onExecute(SysEventType pEventType, Object pArgs) {  
                    Object[] _args = (Object[])pArgs;  
                    int requestCode = (Integer)_args[0];  
                    int resultCode = (Integer)_args[1];  
                    Intent data = (Intent)_args[2];  
                    if(pEventType == SysEventType.OnActivityResult){  
                        _app.unregisterSysEventListener(this, SysEventType.OnActivityResult);  
                        if (requestCode == REQUESTCODE) {  
                            if(resultCode == Activity.RESULT_OK){  
                                String phoneNumber = null;  
                                String resultString = "";  
                                Context context = pWebview.getContext();  
                                Uri contactData = data.getData();  
                                Cursor cursor = context.getContentResolver().query(contactData, null, null, null, null);  
                                cursor.moveToFirst();  
                                String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  
                                resultString += "{\"givenName\":\"" + givenName + "\",\"numbers\":[";  
                                String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
                                Cursor pCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
                                        null,   
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,   
                                        null,   
                                        null);  
                                while (pCursor.moveToNext()) {  
                                    phoneNumber = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
                                    resultString += "{\"value\":\"" + phoneNumber + "\"},";  
                                }  
                                resultString = resultString.substring(0, resultString.length()-1);  
                                resultString += "]}";  
                                cursor.close();  
                                pCursor.close();  
                                JSUtil.execCallback(pWebview, callBackId, JSONUtil.createJSONObject(resultString), JSUtil.OK, false);  
                            }  
                        }  
                    }  
                    return false;  
                }  

            }, SysEventType.OnActivityResult);  
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
            pWebview.getActivity().startActivityForResult(intent, REQUESTCODE);  
        }  
        return null;  
    }  

    @Override  
    public void init(AbsMgr arg0, String arg1) {  
        // TODO Auto-generated method stub  
    }  
}
10 关注 分享
Android_磊子 半杯可乐 DCloud_heavensoft outofMemory 踩着单车载着猪 Jerry_Yee 张释 moliu sunshine2651 飞apple55

要回复文章请先登录注册

Marco

Marco

虽然分享了,依然不知所云。
2018-06-15 11:32
霸王硬上车

霸王硬上车

运行时显示“打包是未添加nativecontact模块”
2017-11-30 15:09
大金子

大金子

谁能给这个文档里的内容及文件给出一个详细的解释?
2017-05-16 11:57
passage2011

passage2011

回复 闪闪 :
插件开发已经掌握,谢谢!
2017-03-27 13:35
小牛_ge

小牛_ge

请问 通过原生开发的 某个功能模块,可以通过这种插件的形式 嵌入到 5+ 环境中吗?
2017-03-08 16:52
闪闪

闪闪 (作者)

回复 飞apple55 :
这样的话,只能离线打包,不能在hbuilder中打包
2017-02-03 16:42
飞apple55

飞apple55

回复 闪闪 :
这样调插件需要离线打包吗?还是可以在hbuilder里打包?现在IOS也想调用自己写的类,不知道怎么弄,想知道不用离线打包的方式有没有办法引用自己写的类
2017-01-13 14:29
闪闪

闪闪 (作者)

回复 GG虐MM :
需要自己写
2017-01-13 12:53
GG虐MM

GG虐MM

请问你上面那个NativeContact.java文件是需要自己写的还是第三方插件里面有的java文件?
2016-11-30 16:02
闪闪

闪闪 (作者)

回复 passage2011 :
好久没有搞了,抱歉,找不到原来的demo了
2016-11-22 11:16