acntw 2013-05-03
文章源自:http://blog.csdn.net/phonegapcn/article/details/6695642
contacts对象提供对设备通讯录数据库的访问。
contacts.create
返回一个新的Contact对象。
var contact = navigator.service.contacts.create(properties);
说明:
简单的范例:
var myContact = navigator.service.contacts.create({"displayName": "Test User"});
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { var myContact = navigator.service.contacts.create({"displayName": "Test User"}); myContact.gender = "male"; console.log("The contact, " + myContact.displayName + ", is of the " + myContact.gender + " gender"); } </script> </head> <body> <h1>Example</h1> <p>Create Contact</p> </body> </html>
contacts.find
查询设备通讯录数据库,并返回包含指定字段的一个或多个Contact对象。
navigator.service.contacts.find(contactFields, contactSuccess, contactError, contactFindOptions);
说明:
简单的范例:
function onSuccess(contacts) { alert('Found ' + contacts.length + ' contacts.'); } function onError() { alert('onError!'); } // 从所有联系人中查找任意名字字段中包含“Bob”的联系人 var options = new ContactFindOptions(); options.filter="Bob"; var fields = ["displayName", "name"]; navigator.service.contacts.find(fields, onSuccess, onError, options);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // PhoneGap加载完毕 function onDeviceReady() { // 从所有联系人中查找任意名字字段中包含“Bob”的联系人 var options = new ContactFindOptions(); options.filter="Bob"; var fields = ["displayName", "name"]; navigator.service.contacts.find(fields, onSuccess, onError, options); } // onSuccess: 获取当前联系人结果集的快照 function onSuccess(contacts) { for (var i=0; i<contacts.length; i++) { console.log("Display Name = " + contacts[i].displayName); } } // onError: 获得联系人失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
Contact
包含描述联系人的属性,比如用户的个人或者商务联系方式。
Save 简单的范例:
function onSuccess(contacts) { alert("Save Success"); } function onError(contactError) { alert("Error = " + contactError.code); } // 建立一个新的联系人对象 var contact = navigator.service.contacts.create(); contact.displayName = "Plumber"; contact.nickname = "Plumber"; //同时指定以支持所有设备 // 填充一些字段 var name = new ContactName(); name.givenName = "Jane"; name.familyName = "Doe"; contact.name = name; // 存储到设备上 contact.save(onSuccess,onError);
Clone 简单的范例:
// 克隆联系人对象 var clone = contact.clone(); clone.name.givenName = "John"; console.log("Original contact name = " + contact.name.givenName); console.log("Cloned contact name = " + clone.name.givenName);
Remove 简单的范例:
function onSuccess() { alert("Removal Success"); } function onError(contactError) { alert("Error = " + contactError.code); } // 从设备上删除该联系人 contact.remove(onSuccess,onError);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { // 创建联系人 var contact = navigator.service.contacts.create(); contact.displayName = "Plumber"; contact.nickname = "Plumber"; //同时指定以支持所有设备 var name = new ContactName(); name.givenName = "Jane"; name.familyName = "Doe"; contact.name = name; // 存储联系人 contact.save(onSaveSuccess,onSaveError); // 克隆联系人 var clone = contact.clone(); clone.name.givenName = "John"; console.log("Original contact name = " + contact.name.givenName); console.log("Cloned contact name = " + clone.name.givenName); // 删除联系人 contact.remove(onRemoveSuccess,onRemoveError); } // onSaveSuccess: 返回当前保存成功的联系人数据的快照 function onSaveSuccess(contacts) { alert("Save Success"); } // onSaveError: 获取联系人数据失败 function onSaveError(contactError) { alert("Error = " + contactError.code); } // onRemoveSuccess: 返回当前删除成功的联系人数据的快照 function onRemoveSuccess(contacts) { alert("Removal Success"); } // onRemoveError: 获取联系人数据失败 function onRemoveError(contactError) { alert("Error = " + contactError.code); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
ContactAddress
包含Contact对象的地址属性。
简单的范例:
// 显示所有联系人的地址信息 function onSuccess(contacts) { for (var i=0; i< contacts.length; i++) { for (var j=0; j< contacts[i].addresses.length; j++) { alert("Pref: " + contacts[i].addresses[j].pref + "\n" + "Type: " + contacts[i].addresses[j].type + "\n" + "Formatted: " + contacts[i].addresses[j].formatted + "\n" + "Street Address: " + contacts[i].addresses[j].streetAddress + "\n" + "Locality: " + contacts[i].addresses[j].locality + "\n" + "Region: " + contacts[i].addresses[j].region + "\n" + "Postal Code: " + contacts[i].addresses[j].postalCode + "\n" + "Country: " + contacts[i].addresses[j].country); } } } function onError(contactError) { alert('onError!'); } // 从全部联系人中查找 var options = new ContactFindOptions(); options.filter=""; var filter = ["displayName","addresses"]; navigator.contacts.find(filter, onSuccess, onError, options);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { // 从全部联系人中进行搜索 var options = new ContactFindOptions(); options.filter=""; var filter = ["displayName","addresses"]; navigator.service.contacts.find(filter, onSuccess, onError, options); } // onSuccess: 返回当前联系人结果集的快照 function onSuccess(contacts) { // 显示所有联系人的地址信息 for (var i=0; i<contacts.length; i++) { for (var j=0; j<contacts[i].addresses.length; j++) { alert("Pref: " + contacts[i].addresses[j].pref + "\n" + "Type: " + contacts[i].addresses[j].type + "\n" + "Formatted: " + contacts[i].addresses[j].formatted + "\n" + "Street Address: " + contacts[i].addresses[j].streetAddress + "\n" + "Locality: " + contacts[i].addresses[j].locality + "\n" + "Region: " + contacts[i].addresses[j].region + "\n" + "Postal Code: " + contacts[i].addresses[j].postalCode + "\n" + "Country: " + contacts[i].addresses[j].country); } } } // onError: 获取联系人结果集失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
ContactField
简单的范例:
// 建立一个新的联系人记录 var contact = navigator.service.contacts.create(); // 存储联系人电话号码到ContactField[]数组 var phoneNumbers = [3]; phoneNumbers[0] = new ContactField('work', '212-555-1234', false); phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // 首选项 phoneNumbers[2] = new ContactField('home', '203-555-7890', false); contact.phoneNumbers = phoneNumbers; // 保存联系人 contact.save();
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { // 建立一个新的联系人记录 var contact = navigator.service.contacts.create(); //存储联系人电话号码到ContactField[]数组 var phoneNumbers = [3]; phoneNumbers[0] = new ContactField('work', '212-555-1234', false); phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // 首选项 phoneNumbers[2] = new ContactField('home', '203-555-7890', false); contact.phoneNumbers = phoneNumbers; // 存储联系人 contact.save(); // 搜索联系人列表,返回符合条件联系人的显示名及电话号码 var options = new ContactFindOptions(); options.filter=""; filter = ["displayName","phoneNumbers"]; navigator.service.contacts.find(filter, onSuccess, onError, options); } // onSuccess:返回联系人结果集的快照 function onSuccess(contacts) { for (var i=0; i< contacts.length; i++) { // 显示电话号码 for (var j=0; j< contacts[i].phoneNumbers.length; j++) { alert("Type: " + contacts[i].phoneNumbers[j].type + "\n" + "Value: " + contacts[i].phoneNumbers[j].value + "\n" + "Preferred: " + contacts[i].phoneNumbers[j].pref); } } } // onError: 获取联系人结果集失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
ContactFindOptions
简单的范例:
// 成功后的回调函数 function onSuccess(contacts) { for (var i=0; i< ;contacts.length; i++) { alert(contacts[i].displayName); } } // 出错后的回调函数 function onError() { alert('onError!'); } // 指定联系人搜索条件 var options = new ContactFindOptions(); options.filter=""; // 空搜索字符串将返回所有联系人 options.multiple=true; // 可返回多条记录 filter = ["displayName"]; // 仅返回contact.displayName字段 // 查找联系人 navigator.service.contacts.find(filter, onSuccess, onError, options);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { // 指定联系人搜索条件 var options = new ContactFindOptions(); options.filter=""; // 空搜索字符串将返回所有联系人 options.multiple=true; // 可返回多条记录 filter = ["displayName"]; // 仅返回contact.displayName字段 // 查找联系人 navigator.service.contacts.find(filter, onSuccess, onError, options); } // onSuccess: 返回当前联系人记录集的快照 function onSuccess(contacts) { for (var i=0; i< contacts.length; i++) { alert(contacts[i].displayName); } } // onError: 获取联系人失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
ContactName
简单的范例:
function onSuccess(contacts) { for (var i=0; i< ;contacts.length;i++) { alert('Formatted: ' + contacts[i].name.formatted + '\n' + 'Family Name: ' + contacts[i].name.familyName + '\n' + 'Given Name: ' + contacts[i].name.givenName + '\n' + 'Middle Name: ' + contacts[i].name.middleName + '\n' + 'Suffix: ' + contacts[i].name.honorificSuffix + '\n' + 'Prefix: ' + contacts[i].name.honorificSuffix); } } function onError() { alert('onError!'); } var options = new ContactFindOptions(); options.filter=""; filter = ["displayName","name"]; navigator.service.contacts.find(filter, onSuccess, onError, options);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { var options = new ContactFindOptions(); options.filter=""; filter = ["displayName"]; navigator.service.contacts.find(filter, onSuccess, onError, options); } // onSuccess: 返回联系人结果集的快照 function onSuccess(contacts) { for (var i=0; i< contacts.length; i++) { alert("Formatted: " + contacts[i].name.formatted + "\n" + "Family Name: " + contacts[i].name.familyName + "\n" + "Given Name: " + contacts[i].name.givenName + "\n" + "Middle Name: " + contacts[i].name.middleName + "\n" + "Suffix: " + contacts[i].name.honorificSuffix + "\n" + "Prefix: " + contacts[i].name.honorificPrefix); } } // onError: 获取联系人结果集失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
ContactOrganization
简单的范例:
function onSuccess(contacts) { for (var i=0; i< contacts.length; i++) { for (var j=0; j< contacts[i].organizations.length; j++) { alert("Pref: " + contacts[i].organizations[j].pref + "\n" + "Type: " + contacts[i].organizations[j].type + "\n" + "Name: " + contacts[i].organizations[j].name + "\n" + "Department: " + contacts[i].organizations[j].department + "\n" + "Title: " + contacts[i].organizations[j].title); } } } function onError() { alert('onError!'); } var options = new ContactFindOptions(); options.filter=""; filter = ["displayName","organizations"]; navigator.service.contacts.find(filter, onSuccess, onError, options);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { var options = new ContactFindOptions(); options.filter=""; filter = ["displayName","organizations"]; navigator.service.contacts.find(filter, onSuccess, onError, options); } // onSuccess: 返回当前联系人结果集的快照 function onSuccess(contacts) { for (var i=0; i<contacts.length; i++) { for (var j=0; j<contacts[i].organizations.length; j++) { alert("Pref: " + contacts[i].organizations[j].pref + "\n" + "Type: " + contacts[i].organizations[j].type + "\n" + "Name: " + contacts[i].organizations[j].name + "\n" + "Department: " + contacts[i].organizations[j].department + "\n" + "Title: " + contacts[i].organizations[j].title); } } } // onError:获取联系人失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>Find Contact</p> </body> </html>
ContactError
contactSuccess
contacts.find操作成功时调用的回调函数,同时返回查找到的Contact数组。
范例:
function contactSuccess(contacts) { for (var i=0; i< contacts.length; i++) { console.log(“Display Name = ” + contacts[i].displayName; } }
contactError
操作联系人出错时的回调函数。
contactFields
contacts.find方法的必填参数,该参数定义了查找操作返回的Contact对象中应该包含哪些字段。
["name", "phoneNumbers", "emails"]
contactFindOptions
contats.find方法的可选参数,通过该参数从通讯录数据库中筛选联系人。
{ filter: "", multiple: true }