Phonegap联系人 获取Contacts HTML5,android

Phonegap 获取 所有联系人  此版本是cordova 3.4的

调用系统的API 经过修改  迭代出所有联系人  贴上完整代码

 

文章结尾 提供Demo下载

 

以下介绍的 phonegap contacts 的 API有

methods

Arguments

Object

 

 

从建立项目说起:

<1> 在控制台    创建一个phonegap工程  命令如下

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. phonegap create my-app  
  2. cd my-app  
  3. phonegap run android  

 

<2> 我们从命令行进入 到工程目录下的  plugins文件夹

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. cd my-app  
  2.   
  3. cd plugins  


 

 

<3> 现在开始下载插件 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. cordova plugin add org.apache.cordova.contacts  

 

 

<4> 添加android 平台工程  (ios把 "android" 替换)

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. cordova platform add android  

 

 

<5> 编译android工程

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. cordova build  


 

 

至此  contacts 插件已经生成并添加到项目当中去...

现在项目应该 有如下文件:

 

 

cordova-plugins.js 里面的文件是:

 

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. cordova.define('cordova/plugin_list'function(require, exports, module) {  
  2. module.exports = [  
  3.     {  
  4.         "file""plugins/org.apache.cordova.contacts/www/contacts.js",  
  5.         "id""org.apache.cordova.contacts.contacts",  
  6.         "clobbers": [  
  7.             "navigator.contacts"  
  8.         ]  
  9.     },  
  10.     {  
  11.         "file""plugins/org.apache.cordova.contacts/www/Contact.js",  
  12.         "id""org.apache.cordova.contacts.Contact",  
  13.         "clobbers": [  
  14.             "Contact"  
  15.         ]  
  16.     },  
  17.     {  
  18.         "file""plugins/org.apache.cordova.contacts/www/ContactAddress.js",  
  19.         "id""org.apache.cordova.contacts.ContactAddress",  
  20.         "clobbers": [  
  21.             "ContactAddress"  
  22.         ]  
  23.     },  
  24.     {  
  25.         "file""plugins/org.apache.cordova.contacts/www/ContactError.js",  
  26.         "id""org.apache.cordova.contacts.ContactError",  
  27.         "clobbers": [  
  28.             "ContactError"  
  29.         ]  
  30.     },  
  31.     {  
  32.         "file""plugins/org.apache.cordova.contacts/www/ContactField.js",  
  33.         "id""org.apache.cordova.contacts.ContactField",  
  34.         "clobbers": [  
  35.             "ContactField"  
  36.         ]  
  37.     },  
  38.     {  
  39.         "file""plugins/org.apache.cordova.contacts/www/ContactFindOptions.js",  
  40.         "id""org.apache.cordova.contacts.ContactFindOptions",  
  41.         "clobbers": [  
  42.             "ContactFindOptions"  
  43.         ]  
  44.     },  
  45.     {  
  46.         "file""plugins/org.apache.cordova.contacts/www/ContactName.js",  
  47.         "id""org.apache.cordova.contacts.ContactName",  
  48.         "clobbers": [  
  49.             "ContactName"  
  50.         ]  
  51.     },  
  52.     {  
  53.         "file""plugins/org.apache.cordova.contacts/www/ContactOrganization.js",  
  54.         "id""org.apache.cordova.contacts.ContactOrganization",  
  55.         "clobbers": [  
  56.             "ContactOrganization"  
  57.         ]  
  58.     }  
  59. ];  
  60. module.exports.metadata =   
  61. // TOP OF METADATA  
  62. {  
  63.     "org.apache.cordova.contacts""0.2.8"  
  64.    }  
  65. // BOTTOM OF METADATA  
  66. });  


 

res/xml 目录下 的config.xml

 

 

在config.xml 里面有

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <feature name="Contacts">  
  2.     <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />  
  3. </feature>  


 

 

 

现在大家只需要将工程导入到eclipse当中 使用官方APi语句在javascript中调用

 

将下方语句考到   assets目录下  www/index.html  当中  完全复制过去;

 

example:

 

 

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>     
  3. <head>        
  4.  <title>Contact Example</title>      
  5.  <script type="text/javascript" charset="utf-8" src="cordova.js"></script>    
  6.  <script type="text/javascript" charset="utf-8">            
  7.   // Wait for device API libraries to load           
  8.   document.addEventListener("deviceready", onDeviceReady, false);        
  9.     // device APIs are available    
  10.   
  11.     function onDeviceReady() {           
  12.     // find all contacts with ' ' in any name field    ,field 如果为空 则返回全部的  
  13.     var options = new ContactFindOptions();      
  14.     options.filter = "";     
  15.     options.multiple = true;  
  16.     /* 
  17.     查找关键字   
  18.     名字: "displayName"  , 
  19.     电话号码:"phoneNumbers"   //ContactField[]类型  
  20.  
  21.     */    
  22.     var fields = ["displayName""name""phoneNumbers"];                  
  23.     navigator.contacts.find(fields, onSuccess, onError, options);            }   
  24.       
  25.   
  26.     // onSuccess: Get a snapshot of the current contacts          
  27.     function onSuccess(contacts) {       
  28.   
  29.       // 联系人与电话号码 全写在这儿  
  30.       var aResult = [];      
  31.   
  32.       for (var i = 0; contacts[i]; i++) {                      
  33.         console.log("Display Name = " + contacts[i].displayName);   
  34.   
  35.         if (contacts[i].phoneNumbers && contacts[i].phoneNumbers.length) {  
  36.   
  37.           var contactPhoneList =[];  
  38.   
  39.           for (var j = 0; contacts[i].phoneNumbers[j]; j++) {  
  40.          // alert(contacts[i].phoneNumbers[j].type+"    "+contacts[i].displayName+"---------" + contacts[i].phoneNumbers[j].value );  
  41.   
  42.          contactPhoneList.push(  
  43.          {   
  44.           'type' :  contacts[i].phoneNumbers[j].type,   
  45.           'value' : contacts[i].phoneNumbers[j].value  
  46.         }  
  47.         );  
  48.   
  49.        };          
  50.   
  51.        aResult.push({  
  52.         name:contacts[i].displayName,  
  53.         phone:contactPhoneList  
  54.       });  
  55.   
  56.      };  
  57.         //  
  58.       }  
  59.   
  60.         //迭代获取 联系人和号码  
  61.     for (var i = 0; aResult[i]; i++) {  
  62.       for (var j = 0 ; aResult[i].phone[j]; j++) {  
  63.   
  64.         alert(aResult[i].name +"--------"+ aResult[i].phone[j].type+"-----"+aResult[i].phone[j].value  
  65.           );  
  66.       };  
  67.   
  68.   
  69.     };  
  70.   }     
  71.     // onError: Failed to get the contacts     
  72.     function onError(contactError) {                alert('onError!');            }     
  73.   
  74.     function baozi(){ alert("S1");}  
  75.   
  76.     function intent() {       onDeviceReady();     }      
  77.   
  78.   
  79.   </script>     
  80. </head>     
  81. <body>       
  82.  <h1>Example</h1>      
  83.  <p>Find Contacts</p>    
  84.   
  85.  <p><a href="#" οnclick="showAlert(); return false;">Show Alert</a></p>      
  86.  <p><a href="#" οnclick="playBeep(); return false;">Play Beep</a></p>      
  87.  <p><a href="#" οnclick="baozi(); return false;">Vibrate</a></p>    
  88.  <p><a href="#" οnclick="intent(); return false;">Html跳转到android界面</a></p>     
  89. </body>  
  90. </html>  


 

代码详解:

上段代码  中的

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var options = new ContactFindOptions();      
  2.     options.filter = "";     
  3.     options.multiple = true;  
  4.     var fields = ["displayName",  "phoneNumbers"];                  
  5.     navigator.contacts.find(fields, onSuccess, onError, options);            }   


options.filter   //过滤条件

options.multiple   //是否 查询多个

fields        //将要查询的 关键字

navigator.contacts.find (fields,onSuccess,onError , options  ); // 查找操作

其中的onSuccess 是成功找到联系人后 返回后将执行的回调 

 

 


   
    查找关键字  
    名字: "displayName"  ,
    电话号码:"phoneNumbers"   //ContactField[]类型

   

 

 

在注释之前  是js里面获取 所有联系的方式  

注释之后是 方便测试的弹出窗口

 

 

点击 "   Html跳转到android界面  "    就会弹出  alert   如下:

 

迭代 弹出所有联系人

 

 

 

至此 获取了所有的电话号码

 

顺道说一句

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (contacts[i].phoneNumbers && contacts[i].phoneNumbers.length) {}  


 

上面这一句是判断  联系人是否为空

 

 

 

工程下载    将phonegap的platforms导入到eclipse中  

如果报错clear一下  查看导的lib包 有没有报错

如果还有错  那么就是您选用了  google的API   改成最新版的android  API 就好了

如果导入工程遇到问题 可以查阅我此篇文章

Blog:  http://blog.csdn.net/aaawqqq/article/details/20463183

Phonegap解决错误:Error initializing Cordova:Class not found:

http://blog.csdn.net/aaawqqq/article/details/21243869

 

Demo 下载地址:

http://download.csdn.net/detail/aaawqqq/7053769

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值