【问题标题】:How can I programmatically get the contact list of Android/iPhone using titanium如何使用钛以编程方式获取 Android/iPhone 的联系人列表
【发布时间】:2013-03-27 19:58:55
【问题描述】:

如何使用titanium 以编程方式获取 Android/iPhone 的联系人列表,我需要这个用于只读目的。

我已经检查了这个Can I get a user's phone number using any of the Titanium API's?,但我喜欢使用我的应用程序打开它,然后拨打所选号码的电话,我的应用程序不会对该号码执行任何其他操作(编辑、删除)。在使用钛的 Android 和 iPhone 或简单的 Android 和 iPhone 中这是否可能?

【问题讨论】:

    标签: android iphone titanium titanium-mobile


    【解决方案1】:
    $.index.open();
    var people = Titanium.Contacts.getAllPeople();
    var totalContacts = people.length;
    Ti.UI.setBackgroundColor('#F0FFFF');
    var data = [];
        var win = Ti.UI.createWindow({
        backgroundColor : 'white',
    });
    
    var view = Ti.UI.createView({
        height : "50dp",
        width : "100%",
        top : '0dp',
        backgroundColor : '#050505',
    });
    
    var text = Ti.UI.createLabel({
        text : "Contact Book",
        left : 20,
        color : '#fff'
    
    });
    
    view.add(text);
    win.add(view);
    
    var template = {
        childTemplates : [{
            type : 'Ti.UI.Button',
            bindId : 'image',
            properties : {
                left : '2dp',
                backgroundImage : 'appicon.png',
            }
        }, {
            type : 'Ti.UI.Label',
            bindId : 'rowtitle',
            properties : {
                left : '70dp'
            }
        }]
    };
    if( totalContacts > 0 )
    { 
        for( var index = 0; index < totalContacts; index++ )
        {
             var person = people[index];
            Titanium.API.info(person.fullName);
            //table.add(person.fullName);
            if(person.fullName != null){
                data.push({
                    rowtitle : {
                        text :person.fullName
                    },
    
                });
            }
        }
    }
    
    var listView = Ti.UI.createListView({
        top : '55dp',
        templates : {
            'plain' : template
        },
    
        defaultItemTemplate : 'plain',
    });
    
    var section = Ti.UI.createListSection({
        items : data
    });
    
    listView.sections = [section];
    
    win.add(listView);
    win.open();
    

    【讨论】:

      【解决方案2】:

      您可以使用showContacts方法获取您的手机联系人,如下所示

      Titanium.Contacts.showContacts(); //Will display the native contacts in both iphone and android
      

      如果是 iphone,您应该需要联系人的访问权限。您可以按如下方式检查权限

      if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_AUTHORIZED){
          //You've authorization
          //Some code here
      } else if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_UNKNOWN){
          Ti.Contacts.requestAuthorization(function(e){
          //Authorization is unknown so requesting for authorization
          if (e.success) {
                  //You've authorization
                  //Some code here
              } else {
                  //No authorization hence you cannot access contatcs
              }
          });
      } else {
          //No authorization hence you cannot access contatcs
      }
      

      更多详情请咨询Titanium Contacts module

      Ti.Contacts.getAllPeople 也会这样做,也请参考this 链接

      【讨论】:

        【解决方案3】:

        首先,您需要按照here 所述访问联系人。

        之后可以通过getAllPeople获取所有联系人

        这适用于 iOS 和 Android。

        在 Android 上拨打电话很简单(您应该使用Ti.Android.ACTION_DIAL 创建电话呼叫Intent)。在 iOS 上,您只需要显示号码,系统就会将其链接到呼叫操作。如果没有,您可以向元素添加监听器:

        label.addEventListener('click', function(e) {
          Ti.Platform.openURL('tel:<number');
        });
        

        【讨论】:

        • 请注意,在 Android 中,getAllPeople() 非常慢并且消耗大量内存,因为每个返回的人都有自己的代理。如果您只需要特定信息,我建议您编写一个原生模块。
        猜你喜欢
        • 1970-01-01
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 2012-08-12
        • 2016-12-26
        • 2023-02-02
        相关资源
        最近更新 更多