La llamada no funciona en Android usando Cordova, tomando contactos del teléfono

I want to call a phone number from clicking on a link, but the contact should be retrieved from phone. So I implemented a code for this. But tel is opening dialer, but my retrieved contact number is missing. Any suggestion?

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="jquery.mobile-1.1.0.min.css" />
    <script src="jquery-1.7.1.min.js"></script>
    <script src="jquery.mobile-1.1.0.min.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Cordova is ready
    //
    function onDeviceReady() {
        // specify contact search criteria
        var options = new ContactFindOptions();
        options.filter="";          // empty search string returns all contacts
        options.multiple=true;      // return multiple results
        filter = ["displayName"];   // return contact.displayName field

        // find contacts
        navigator.contacts.find(filter, onSuccess, onError, options);
    }

    // onSuccess: Get a snapshot of the current contacts
    //
    function onSuccess(contacts) {
        for (var i=0; i<contacts.length; i++) {
            alert(contacts[i].displayName);
            var phonenumber;
            phonenumber = contacts[i].phoneNumbers;
            $("ul").append("<li><a href=tel:"+phonenumber+">"+contacts[i].displayName+"</a></li>");
        }
    };

    // onError: Failed to get the contacts
    //
    function onError(contactError) {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <ul data-role="listview" data-theme="g" data-filter="true">
        <li><a>hello</a></li>
    </ul>
  </body>
</html>

preguntado el 12 de junio de 12 a las 10:06

3 Respuestas

contacts[i].phoneNumbers is an array, so you should add it the variable:

var phonenumber = contacts[i].phoneNumbers[0];

or loop through that array, which ever suits you best.

Respondido el 12 de junio de 12 a las 11:06

Also, you should double check that contacts[i].phoneNumbers is not null which it will be if the contact has no phone numbers. - Simón MacDonald

Your filter is wrong. You need to make it: filter = ["displayName", "phoneNumbers"]; this will return an array of contacts with their displayName and phoneNumbers populated. - Simón MacDonald

Prueba con

$("ul").append("<li><a href=callto:"+phonenumber+">"+contacts[i].displayName+"</a></li>");

Respondido el 12 de junio de 12 a las 13:06

Esto funcionó para mí:

phonenumber = contacts[i].phoneNumbers[0].value.replace(" ", "");

$("#ul").append("<li><a href=tel:+91"+phonenumber.replace(" ", "")+">"+contacts[i].displayName+"<br/>"+phonenumber.replace(" ", "")+"</a></li><br/>");

I used replace() function because i am getting space in between the digits of phone number.

Espero que te ayude.

Respondido 29 Abr '14, 07:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.