javascript 使用 Phonegap 在 Android 上访问联系人
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10268122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access contacts on Android using Phonegap
提问by Sana Joseph
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. So, do you know what's wrong with my code & why isn't it working?
您知道我是否应该添加任何类型的插件或更新任何内容以便这些功能可以工作?我正在使用cordova 1.6.1 版,并且更新了清单中的权限以便能够访问联系人。那么,您知道我的代码有什么问题吗?为什么它不起作用?
Thanks a lot.
非常感谢。
回答by Paul Beusterien
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');
}
回答by Simon MacDonald
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.
反而。