contacts.find在Android 4(phonegap)上不起作用

时间:2016-12-27 16:06:41

标签: javascript android cordova phonegap-plugins cordova-plugins

我正在研究从手机上读取联系人的Android应用程序(phonegap),它在Android 5及更高版本上工作正常但是当涉及到android 4操作系统时,它无法从手机中读取联系人,所以我想问一下如果有人在使用phone.apind中的contacts.find时遇到此问题。

document.addEventListener("deviceready", onDeviceReady, true); //the onDeviceReady function takes place when the app starts

function onDeviceReady() {
// find all contacts displayName, Name and phoneNumbers
var fields       = ["displayName", "name", "phoneNumbers"];
var options      = new ContactFindOptions();
options.filter   = "";
options.multiple = true;
navigator.contacts.find(fields, onSuccess, onError, options);
	var currentdate = new Date();
    var datetime = currentdate.getDate() + "-"+(currentdate.getMonth()+1) 
    + "-" + currentdate.getFullYear() + "T" + currentdate.getHours() + ":" 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds();
	alert(datetime);
}	

1 个答案:

答案 0 :(得分:0)

我为你创建了样本,并在Android 4.1.1,4.2.2,4.4.4中进行了测试,并且它的工作非常好。

代码是::

首先添加联系人插件 cordova plugin add cordova-plugin-contacts

然后在index.html ::

中写入
<body>
    <h2>Contacts List</h2>
    <div id="mobile"></div>
    <div id="email"></div>

    <script src="cordova.js"></script>

    <script type="text/javascript">
        document.addEventListener("deviceready", init, false);
        function init() {
            navigator.contacts.find([navigator.contacts.fieldType.displayName],gotContacts,errorHandler);
        }

        function errorHandler(e) {
            console.log("errorHandler: "+e);
        }

        function gotContacts(c) {
            console.log("gotContacts, number of results "+c.length);

            mobileDiv = document.querySelector("#mobile");
            emailDiv = document.querySelector("#email");

            /* Retriving phoneNumbers */
            for(var i=0, len=c.length; i<len; i++) {
                if(c[i].phoneNumbers && c[i].phoneNumbers.length > 0) {
                    mobileDiv.innerHTML += "<p>"+c[i].displayName+"<br/>"+c[i].phoneNumbers[0].value+"</p>";
                }
            }

            /* Retriving Email */
            for(var i=0, len=c.length; i<len; i++) {
                if(c[i].emails && c[i].emails.length > 0) {
                    emailDiv.innerHTML += "<p>"+c[i].emails[0].value+"</p>";
                }
            }
        }
    </script>
</body>