获取Titanium中的电话号码(android)

时间:2012-10-13 14:45:22

标签: javascript titanium appcelerator titanium-mobile appcelerator-mobile

如何在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 "" :(

2 个答案:

答案 0 :(得分:0)

尝试以下代码,它与我合作

//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模拟器的屏幕截图。试试这样的数字

enter image description here

答案 1 :(得分:0)

这段代码对我有用。它会扫描电话簿中的所有联系号码,无论是移动电话还是家庭电话簿或其他任何内容。代码也会从数字中删除所有非数字字符:

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, "");
        }
    }
}