获取手机通讯录
参照http://docs.wex5.com/cordova-plugin-device/调用获取device属性时提示'device' is not defined,哪里还需要设置或者导入么?测试代码如下:define(function(require){ var $ = require("jquery"); var justep = r
'device' is not defined,哪里还需要设置或者导入么?
测试代码如下:
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
require("$UI/system/lib/cordova/cordova");
require("cordova!cordova-plugin-device");
var Model = function(){
this.callParent();
};
Model.prototype.button1Click = function(event){
alert( device.platform + " " + device.cordova);
};
return Model;
});
//查找联系人
要在Contacts数据库上进行搜索,可使用contacts.find()方法,如下所示:
navigator.contacts.find(contactFields, onSuccess, onError, options);
这个例子显示了如下内容:
第1个参数用来给方法传递一组联系人字段。这些字段也将被返回。例如,如果想要的是人名、电子邮件地址和电话号码,就应该在这里指明。如果只是想要联系人的id,可以给这个参数传递一个空值。如果想要返回所有字段,可以传递进["*"]。
第2个参数是success回调函数,它将接收一个正确的Contact对象,来保存要查找的联系人信息。
第3个参数是error回调函数,它将包含抛出的任何错误。
第4个参数是一组可以传递给find方法的选项。其中一个最常使用的是filter选项,它是一个可以对Contacts数据库进行过滤查询的字符串。
以下是查找通讯录的一个示例:
function onSuccess(contacts) {
document.write(contacts.length + ' contacts found.');
for (var i=0; i<contacts.length; i++) {
for (var j=0; j<contacts[i].emails.length; j++) {
document.write("Email = " + contacts[i].emails[j].email);
}
}
}
function onError(contactError) {
alert('Ooops!');
}
// 查找电子邮件地址中带有gmail的所有联系人
var myOptions = new ContactFindOptions();
myOptions.filter="gmail";
var myFields = ["emails"];
navigator.contacts.find(myFields, onSuccess, onError, myOptions);
contacts对象提供对设备通讯录数据库的访问。
方法:
contacts.create
contacts.find
参数:
contactFields
contactSuccess
contactError
contactFindOptions
对象:
Contact
ContactName
ContactField
ContactAddress
ContactOrganization
ContactFindOptions
ContactError
例2:
查询:
var options = new ContactFindOptions();
options.filter = "童星强";//查询的通讯录人名字
options.multiple = false;
fields = ["displayName"];
navigator.contacts.find(fields, contactfindSuccess, contactfindError, options);
function contactfindSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
alert("Display Name = " + JSON.stringify(contacts[i]));
}
}
function contactfindError(message) {
alert('Failed because: ' + message);
}
id:全局唯一标识符。(DOMString类型)
displayname:联系人显示名称,适合向最终用户展示的联系人名称。(DOMString类型)
name:联系人姓名所有部分的对象。(CantactName类型)
nickname:昵称,对联系人的非正式称呼。(DOMString类型)
phoneNumbers:联系人所有联系电话的数组。(ContactField[]类型)
emails:联系人所有email地址的数组。(ContactField[]类型)
addrsses:联系人所有联系地址的数组。(ContactAddresses[]类型)
ims:联系人所有IM地址的数组。(ContactField[]类型)
organizations:联系人所属所有组织的数组。(ContactOrganization[]类型)
birthday:联系人的生日。(日期类型)
note:联系人的注释信息。(DOMString类型)
photos:联系人所有照片的数组.(ContactField[]类型)
categories:联系人所属的所有用户自定义类别的数组。(ContactField[]类型)
urls:与联系人相关网页的数组。(ContactField[]类型)
Model.prototype.modelLoad = function(event){
alert("aaa")
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var options = new ContactFindOptions();
options.filter = "";
options.multiple=true;
var fields = ["displayName","phoneNumbers"];
navigator.contacts.find(fields, onSuccess, onError,options);
}
function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
alert("Display Name = " + JSON.stringify(contacts[i]));
alert("displayName = " + contacts[i].displayName);
var phoneNumbers = contacts[i].phoneNumbers;
for(var j = 0;j<phoneNumbers.length;j++)
alert("phoneNumbers= " + phoneNumbers[j].value);
}
}
function onError(contactError) {
alert('onError!');
}
};
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
require("cordova!cordova-plugin-device");
require("cordova!cordova-plugin-contacts");
require("$UI/system/lib/cordova/cordova");
// cordova运行环境,多个页面用cordova插件,每个页面都需要require进来
var Model = function(){
this.callParent();
};
Model.prototype.button1Click = function(event){
//获取手机信息
// document.getElementById("cordovaSpan").innerHTML = device.cordova;
// document.getElementById("platform").innerHTML = device.platform;
// document.getElementById("version").innerHTML = device.version;
// document.getElementById("uuid").innerHTML = device.uuid;
// document.getElementById("model").innerHTML = device.model;
// document.getElementById("manufacturer").innerHTML = device.manufacturer;
alert( device.platform + " " + device.cordova+" "+device.cordova+" "+device.version+" "+device.uuid+" "+device.model+" "+device.manufacturer);
alert("uuid是:"+device.uuid);
};
Model.prototype.button2Click = function(event){
//获取联系人
// alert("aaa")
// document.addEventListener("deviceready", onDeviceReady, false);
// function onDeviceReady() {
// var options = new ContactFindOptions();
// options.filter = "";
// options.multiple=true;
// var fields = ["displayName","phoneNumbers"];
// navigator.contacts.find(fields, onSuccess, onError,options);
// }
// function onSuccess(contacts) {
// for (var i = 0; i < contacts.length; i++) {
// alert("Display Name = " + JSON.stringify(contacts[i]));
// alert("displayName = " + contacts[i].displayName);
// var phoneNumbers = contacts[i].phoneNumbers;
// for(var j = 0;j<phoneNumbers.length;j++)
// alert("phoneNumbers= " + phoneNumbers[j].value);
// }
// }
// function onError(contactError) {
// alert('onError!');
// }
// var options = new ContactFindOptions();
// options.filter = "爸爸";//查询的通讯录人名字
// options.multiple = false;
// fields = ["displayName"];
// navigator.contacts.find(fields, contactfindSuccess, contactfindError, options);
// function contactfindSuccess(contacts) {
// for (var i = 0; i < contacts.length; i++) {
// alert("Display Name = " + JSON.stringify(contacts[i]));
// }
// }
// function contactfindError(message) {
// alert('Failed because: ' + message);
// }
function onSuccess(contacts) {
alert('Found ' + contacts.length + ' contacts.');
}
function onError() {
alert('onError!');
}
// 从所有联系人
var options = new ContactFindOptions();
options.filter="爸爸";
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
};
Model.prototype.button3Click = function(event){
//
var options = new ContactFindOptions();
options.filter = "";
options.multiple=true;
var fields = ["displayName","phoneNumbers"];
navigator.contacts.find(fields, onSuccess, onError,options);
function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
alert("Display Name = " + JSON.stringify(contacts[i]));
alert("displayName = " + contacts[i].displayName);
var phoneNumbers = contacts[i].phoneNumbers;
for(var j = 0;j<phoneNumbers.length;j++)
alert("phoneNumbers= " + phoneNumbers[j].value);
}
}
function onError(contactError) {
alert('onError!');
}
};
return Model;
});
require("$UI/system/lib/cordova/cordova");
require("cordova!cordova-plugin-contacts");//导入cordova
//新增:
var myContact = navigator.contacts.create({"displayName": "Test User"});
myContact.save(contactSuccess, contactError);
function contactSuccess() {
alert("Contact is saved!")
}
function contactError(message) {
alert('Failed because: ' + message);
}
查询:
var options = new ContactFindOptions();
options.filter = "童星强";//查询的通讯录人名字
options.multiple = false;
fields = ["displayName"];
navigator.contacts.find(fields, contactfindSuccess, contactfindError, options);
function contactfindSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
alert("Display Name = " + JSON.stringify(contacts[i]));
}
}
function contactfindError(message) {
alert('Failed because: ' + message);
}
require("cordova!callsplugin");//导入cordova
var firstCall = 0;
var lastCall = 20000 //这里的lastcall就是指通话记录末尾号,你可以通过设置这个,从最近的通话记录开始读取指定个数。
calls.getCalls(function(res) {
var list = JSON.parse(res);
callList = JSON.stringify(list);
// console.log("通话记录:"+calList);
alert(callList);//将所有数据alert出来
}, function(error) {
//console.log(error);
}, "", "getCallDetails", [ firstCall, lastCall ]);
//能查询出拨号人,电话,拨出还是拨入,时间戳,拨号时长。
//具体代码在.java文件中。
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
require("cordova!cordova-plugin-device");
require("cordova!cordova-plugin-contacts");
require("$UI/system/lib/cordova/cordova");
// cordova运行环境,多个页面用cordova插件,每个页面都需要require进来
var Model = function(){
this.callParent();
};
Model.prototype.button1Click = function(event){
//获取手机信息
// document.getElementById("cordovaSpan").innerHTML = device.cordova;
// document.getElementById("platform").innerHTML = device.platform;
// document.getElementById("version").innerHTML = device.version;
// document.getElementById("uuid").innerHTML = device.uuid;
// document.getElementById("model").innerHTML = device.model;
// document.getElementById("manufacturer").innerHTML = device.manufacturer;
alert( device.platform + " " + device.cordova+" "+device.cordova+" "+device.version+" "+device.uuid+" "+device.model+" "+device.manufacturer);
alert("uuid是:"+device.uuid);
};
Model.prototype.button2Click = function(event){
//获取联系人
// alert("aaa")
// document.addEventListener("deviceready", onDeviceReady, false);
// function onDeviceReady() {
// var options = new ContactFindOptions();
// options.filter = "";
// options.multiple=true;
// var fields = ["displayName","phoneNumbers"];
// navigator.contacts.find(fields, onSuccess, onError,options);
// }
// function onSuccess(contacts) {
// for (var i = 0; i < contacts.length; i++) {
// alert("Display Name = " + JSON.stringify(contacts[i]));
// alert("displayName = " + contacts[i].displayName);
// var phoneNumbers = contacts[i].phoneNumbers;
// for(var j = 0;j<phoneNumbers.length;j++)
// alert("phoneNumbers= " + phoneNumbers[j].value);
// }
// }
// function onError(contactError) {
// alert('onError!');
// }
// var options = new ContactFindOptions();
// options.filter = "爸爸";//查询的通讯录人名字
// options.multiple = false;
// fields = ["displayName"];
// navigator.contacts.find(fields, contactfindSuccess, contactfindError, options);
// function contactfindSuccess(contacts) {
// for (var i = 0; i < contacts.length; i++) {
// alert("Display Name = " + JSON.stringify(contacts[i]));
// }
// }
// function contactfindError(message) {
// alert('Failed because: ' + message);
// }
function onSuccess(contacts) {
alert('Found ' + contacts.length + ' contacts.');
}
function onError() {
alert('onError!');
}
// 从所有联系人
var options = new ContactFindOptions();
options.filter="爸爸";
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
};
Model.prototype.button3Click = function(event){
//
var options = new ContactFindOptions();
options.filter = "";
options.multiple=true;
var fields = ["displayName","phoneNumbers"];
navigator.contacts.find(fields, onSuccess, onError,options);
function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
alert("Display Name = " + JSON.stringify(contacts[i]));
alert("displayName = " + contacts[i].displayName);
var phoneNumbers = contacts[i].phoneNumbers;
for(var j = 0;j<phoneNumbers.length;j++)
alert("phoneNumbers= " + phoneNumbers[j].value);
}
}
function onError(contactError) {
alert('onError!');
}
};
return Model;
});
更多推荐


所有评论(0)