【问题标题】:Get Phone Number(android) in Titanium在 Titanium 中获取电话号码(android)
【发布时间】:2012-10-13 14:45:21
【问题描述】:
如何在android中获取电话号码?
示例代码:
var contacts = Titanium.Contacts.getAllPeople();
Titanium.API.log(contacts[0].phone['mobile'][0]); //in iOS, returns "01012345678" fine :)
Titanium.API.log(contacts[0].phone['mobile'][0]); //in Android, returns "" :(
Titanium.API.log(contacts[0].fullName); //in Android & iOS, returns "test Name" fine :)
Titanium.API.log(contacts[0].phone); //in Android, returns "" :(
【问题讨论】:
标签:
javascript
titanium
appcelerator
titanium-mobile
appcelerator-mobile
【解决方案1】:
试试下面的代码,它对我有用
//Getting all the contacts
var people = Ti.Contacts.getAllPeople();
//Getting the total number of contacts
var totalContacts = people.length;
//Checking whether the contact list is empty or not
if( totalContacts > 0 )
{
for( var index = 0; index < totalContacts; index++ )
{
//Holding the details of a single contact
var person = people[index];
Ti.API.info("Mobile -> " + person['phone'].mobile + " home-> " + person['phone'].home);
}
}
请注意,您的手机应在移动和家庭选项中显示联系号码。我从我的 android 模拟器中添加了一个屏幕截图。试着给出这样的数字
【解决方案2】:
这段代码对我有用。它扫描电话簿中的所有联系号码,无论是移动电话还是家庭电话或其他任何电话号码。代码也会从数字中删除所有非数字字符:
var people = Ti.Contacts.getAllPeople();
for (var i=0, ilen=people.length; i<ilen; i++)
{
var person = people[i];
for(var temp in person.phone)
{
var temp_numbers = person.phone[temp];
for(var k=0;k<temp_numbers.length; k++)
{
var temp_num = temp_numbers[k];
temp_num = temp_num.replace(/[^\d.]/g, "");
}
}
}