[英]Access contacts on Android using Phonegap
I'm working on an application using phone-gap. 我正在使用手机间隙开发应用程序。 I'm trying to access the contacts on the mobile coz I'll use them later. 我正试图访问移动设备上的联系人,因为我稍后会使用它们。 I'm now trying to write a code to find contacts on the mobile. 我现在正在尝试编写代码以在移动设备上查找联系人。 Here's the JS file I'm using: 这是我正在使用的JS文件:
alert('Starting JS');
var TAP = ('ontouchend' in window) ? 'touchend' : 'click';
alert('I entered the function');
document.addEventListener('DOMContentLoaded', function () {
alert('I entered the second function');
x$('#friendSubmit').on(TAP, function () {
var filter = x$('#friendName')[0].value;
alert('I entered the third function');
if (!filter)
{
alert('Cant find contacts');
// no contents
return;
}
else
{
findContactByName(filter, function (contacts)
{
alert(contacts.length + ' contact(s) found matching "' +filter + '"');
}
); }
}); });
function findContactByName(name, callback) {
function onError() {
alert('Error: unable to read contacts');
};
var fields = ["displayName", "name"],
options = new ContactFindOptions();
options.filter = name;
options.multiple = true;
// find contacts
navigator.service.contacts.find(fields, callback, onError,
options);
}
None of the alerts are alerted, so it seems that something is wrong in the code (but it alerted when I removed the "findContactByName" function. 没有任何警报被警告,因此似乎代码中出现了问题(但是当我删除“findContactByName”函数时它会发出警报。
Do you know If I should add any kind of plugins, or update anything so that these functions can work ? 你知道吗?如果我应该添加任何类型的插件,或更新任何东西,以便这些功能可以工作? I'm working with cordova version 1.6.1 and I updated the permissions at the manifest to be able to access the contacts. 我正在使用cordova版本1.6.1,我更新了清单上的权限,以便能够访问联系人。 So, do you know what's wrong with my code & why isn't it working? 那么,你知道我的代码有什么问题吗?为什么它不起作用?
Thanks a lot. 非常感谢。
Are you waiting for the deviceready event (PhoneGap loaded)? 您在等待deviceready事件(加载PhoneGap)吗?
The following code works for me to put all contacts with a name field into a names array: 以下代码适用于将带有名称字段的所有联系人放入名称数组中:
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);
}
var names = [];
// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
for (var i=0; i<contacts.length; i++) {
if (contacts[i].displayName) { // many contacts don't have displayName
names.push(contacts[i].displayName);
}
}
alert('contacts loaded');
}
You are working off an old example: 你正在研究一个旧的例子:
navigator.service.contacts.find(fields, callback, onError, options);
hasn't been the right way to call contacts for quite a few releases. 对于很多版本而言,并不是正确的联系方式。 Use: 采用:
navigator.contacts.find(fields, callback, onError, options);
instead. 代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.