Android Apps

Get contacts from Android Phone with image and sort by name – Android PhoneGap App

August 1, 2012 by Sarath DR in Android Apps, Featured with 0 Comments

 Get contacts from Android Phone with image and sort by name   Android PhoneGap App

I had a requirement to fetch the contact details from the phone in an Android phonegap application. But this method is very slow and will not work properly if you have contacts more than 200. This will be useful sometimes if you do not need much operation. Also I am using a imperfect sorting mechanism, please advice me if you have any other solutions.

Let the name of the div to display the contacts be

  <div id="contacts"></div>

Step 1: You can use the below javascript code to fetch contacts from the phone.

1
2
3
4
5
        var options = new ContactFindOptions();
        options.filter="";
        options.multiple=true;
        var filter = [ "displayName", "phoneNumbers", "photos" ];
        navigator.contacts.find(filter, displayContact, onContactError, options);

Step 2: Display function displays the contacts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
   $("#contacts").html("");
 
       var myContacts       = new Object();
 
       // Default image path for the profile image 
       var defaultImagePath = $("#defaultImagePath").attr('src');
 
        for (var i=0; i<contacts.length; i++)
        {
 
            if( contacts[i].phoneNumbers == null )
            continue;
 
           // Checks for the image 
            var img = contacts[i].photos  != null ? contacts[i].photos[0].value : defaultImagePath;
 
            if(contacts[i].phoneNumbers.length)
            for (var j=0; j<contacts[i].phoneNumbers.length; j++)
            {
                var pNumber = contacts[i].phoneNumbers[j].value; 
                var name = contacts[i].displayName != null ? contacts[i].displayName: "No name";
 
               // To sort the names based on the starting letter  
               // Stores the names in that array 
               var index = name.substring(0,1).toUpperCase();
 
               if (typeof myContacts[index] == 'undefined')
               {
                   myContacts[index] = new Array();
               }
 
               // cuts the large names 
               if( name.length > 35 )
               {
                   name = name.substr(0,35)+"...";
               }
              // Push every details into an array. 
              myContacts[index].push({"name":name, "pNumber": pNumber, "img": img} );
 
            }
 
        }
 
          var arrayKeys = new Array(); 
          for (var key in myContacts )
          {
              arrayKeys.push( key );
          }
 
          // Sorts the array based on the key A, B , C etc 
          arrayKeys = arrayKeys.sort();
 
          for( i = 0 ; i < arrayKeys.length ; i++ )
          {
               var records =  myContacts[ arrayKeys[i] ];
               $("#contacts").append ("<li class='letter-head'>"+ arrayKeys[i]+"</li>");
 
             // Sort each names  
             records = records.sort( sortNames );
 
               for( var r_key in records  )
               {
 
 
 
                    $("#contacts").append ( "<li><img src='"+ records[r_key].img+"' /> <span class='contact-name'>"+records[r_key].name + "</span><span class='contact-number'>" + records[r_key].pNumber +  "</span></li>");
               }
 
          }
 
 
            hide_loader();
            $('.addressBook').effect("slide", {direction: "right"}, 500);
 
 
     }
 
  function sortNames(a, b )
     {
         return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
     }

There is an easiest solution if you need to fetch contacts details. I will explain that in my next post.

Tagged , , ,

Share this to

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close