0

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

2 回答 2

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 模拟器中添加了一个屏幕截图。试着给出这样的数字

在此处输入图像描述

于 2012-10-15T07:20:57.407 回答
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, "");
        }
    }
}
于 2012-10-15T13:43:02.273 回答