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

调用系统通讯录选择手机号 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

要回复文章请先登录注册

闪闪

闪闪 (作者)

回复 rjh :
需要的
2015-05-08 11:21
闪闪

闪闪 (作者)

回复 rjh :
需要的
2015-05-08 11:20
rjh

rjh

还要在properties中添加feature吧?
2015-05-07 10:47
闪闪

闪闪 (作者)

回复 波塞冬 :
嗯,那就也用intent传回结果,原理一样
2015-04-03 11:13
波塞冬

波塞冬

回复 闪闪 :
如果有某些东西需要Android本地处理,当然要启动一个activity
2015-03-26 15:30
闪闪

闪闪 (作者)

回复 风一样的男子 :
你自己创建的新activity?没明白使用情景。。。
2015-03-09 16:37
风一样的男子

风一样的男子

问下 如果跳转到新的activity , 如何获取该activity处理的结果?
2015-03-03 17:23
半杯可乐

半杯可乐

感谢分享
2015-01-27 08:56