在Titanium中获取电话号码(android)[英] Get Phone Number(android) in Titanium

本文是小编为大家收集整理的关于在Titanium中获取电话号码(android)的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

推荐答案

尝试以下代码,它与我一起使用

//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模拟器的屏幕截图.只是尝试给出这样的数字

在此处输入图像说明

其他推荐答案

此代码对我有用.它会扫描电话簿中的所有联系电话,无论是移动,家庭还是其他任何联系.代码还从数字中删除了所有非数字字符:

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

本文地址:https://www.itbaoku.cn/post/1938204.html

问题描述

How can I get phone number in android?

Sample Code:

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 "" :(

推荐答案

Try the following code, It worked with me

//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);
    }
}

note that your phone should have contact number in mobile and home options. I've added a screen shot from my android emulator. Just try giving numbers like this

enter image description here

其他推荐答案

This code worked for me. It scans all the contact numbers from the phone book whether it is mobile or home or anything else. Code also removes all non-digit characters from the number too:

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