使用Phonegap访问Android上的联系人
Access contacts on Android using Phonegap
我正在使用phone-gap开发一个应用程序。我正在尝试访问移动上的联系人,因为我稍后会使用它们。我现在正在尝试编写代码以在移动设备上查找联系人。这是我正在使用的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);
}
没有任何警报被提醒,所以代码中似乎有问题(但是当我删除"findContactByName"函数时,它发出了警报。
你知道我是否应该添加任何类型的插件,或更新任何东西以便这些功能可以工作吗?我正在使用 cordova 版本 1.6.1,我更新了清单上的权限以便能够访问联系人。那么,你知道我的代码出了什么问题,为什么它不起作用吗?
多谢。
您是否正在等待设备就绪事件(已加载 PhoneGap)?
以下代码适用于我将带有姓名字段的所有联系人放入名称数组中:
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');
}
您正在处理一个旧示例:
navigator.service.contacts.find(fields, callback, onError, options);
在相当多的版本中,这不是呼叫联系人的正确方式。用:
navigator.contacts.find(fields, callback, onError, options);
相反。
相关文章:
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 在验证和发送邮件后更改联系人表单的 html
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 使用Native Sockets在Android中实现WebSockets
- window.onload没有'无法在Android WebView中工作
- cordova-2.0.0.js不适用于使用phonegap的android
- 如何在Ionic Android中将Javascript注入到web视图中
- android XWalkView内容宽度
- AddEventListener适用于浏览器,但不适用于Android
- HTML锚标记无法在android平台中下载文件
- 如何在Android上运行的PhoneGap中访问联系人的电话号码
- 从数据库PhoneGap/Android恢复联系人
- 使用Phonegap访问Android上的联系人
- 使用phonegap和Sencha Touch访问Android手机联系人
- 如何加载选定的联系人到一个文本框在Android原生应用程序与HTML5构建
- 在非本机应用程序中将号码添加到iPhone或Android手机联系人列表中
- 我们可以使用phonegap访问HTML5和javascript中的android设备的联系人列表、相机图片等原生内容吗
- 无法在android应用程序中搜索联系人