简体   繁体   English

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

[英]contacts.find doesn't work on Android 4 (phonegap)

I am working on an Android app (phonegap) that reads contacts from phones, It works fine on android 5 and above but when it comes to android 4 OS its not able to read the contacts from the phone so I want to ask if anyone has experienced this problem while working with contacts.find in phonegap. 我正在研究一个可从手机读取联系人的Android应用程序(phonegap),它在android 5及更高版本上可以正常工作,但是当谈到android 4操作系统时,它无法从手机上读取联系人,因此我想问问是否有人在使用contacts.find在phonegap中工作时遇到此问题。

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

I created sample for you and tested in android version 4.1.1, 4.2.2, 4.4.4 and its working perfectly fine. 我为您创建了示例,并在android版本4.1.1、4.2.2、4.4.4中进行了测试,其工作情况非常好。

Code is :: 代码是::

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

and then write in index.html :: 然后写在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM