0

我正在從手機讀取聯繫人的Android應用程序(phonegap),它在Android 5及以上版本可以正常工作,但是當涉及到Android 4操作系統它不能夠從手機上讀取聯繫人,所以我想詢問是否有人遇到過這個問題,同時在手機中使用contacts.find。contacts.find doesnt工作和andriod 4(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); 
 
\t var currentdate = new Date(); 
 
    var datetime = currentdate.getDate() + "-"+(currentdate.getMonth()+1) 
 
    + "-" + currentdate.getFullYear() + "T" + currentdate.getHours() + ":" 
 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds(); 
 
\t alert(datetime); 
 
} \t

+0

你能後的代碼片段? –

+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> 

+0

好吧讓我試試你的並比較 –

+0

我注意到use.Capture中的document.addEventListener(「deviceready」,init,false);被設置爲false是否有特定的原因? –

+0

編號 對我們的代碼沒有影響。 – Hiten