简体   繁体   English

如何在使用HTML5构建的Android本机应用程序中将选定的联系人加载到文本框中

[英]How To Load Selected Contact Into a Text Box in a Android Native App Built with HTML5

I am building a HTML5 based Android App for SMS messaging, that is, I build the app using HTML5, CSS, JavaScript, Jquery, then an gonna use Telerik or PhoneGap to convert it to an APK. 我正在构建用于短信的基于HTML5的Android应用,也就是说,我使用HTML5,CSS,JavaScript,Jquery构建该应用,然后使用Telerik或PhoneGap将其转换为APK。

I need to have a feature on the app that will allow users pick a contact form their phone book and have the selected contact loaded in a text box. 我需要在该应用程序上具有一个功能,该功能将允许用户从他们的电话簿中选择联系人,并将所选的联系人加载到文本框中。

I have the following codes somewhere within my HTML to do the job 我在HTML中的某处有以下代码来完成这项工作

    <input type="text" id="number">
    <button id="pickContact">Phonebook</button>

    <script>
var pickContact = document.getElementById('pickCntact')
pickContact.setOnClickListener(new View.OnClickListener() {             
        @Override
        public void onClick(View g) {
            Intent q = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
            startActivityForResult(q, 1001);  

        }
    });


//and to handle the result (which is referenced by 1001) use this:

public void onActivityResult(int reqCode, int resultCode, Intent data) {  
    super.onActivityResult(reqCode, resultCode, data);  

    if (resultCode == Activity.RESULT_OK) {  
        // getting the URI from result for further working
        Uri contactData = data.getData();
        Cursor c =  managedQuery(contactData, null, null, null, null);

        if (c.moveToFirst()) {


          String  id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

          String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));



          if (hasPhone.equalsIgnoreCase("1")) {
          Cursor phones = getContentResolver().query( 
                          ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                          null, null);
                phones.moveToFirst();
                  //this string will hold the contact number
                  String cNumber = phones.getString(phones.getColumnIndex("data1"));
                  document.getElementById('number').value = cNumber;
              }

        }} 
    } 
    </script>

I have also added the necessary permissions on my manifest file, thus. 因此,我还对清单文件添加了必要的权限。

    <uses-permission android:name="android.permission.READ_CONTACTS" /> 

The problem now is that nothing happens when i click the "phonebok" button. 现在的问题是,当我单击“ phonebok”按钮时,什么也没有发生。

Please could anyone help me out. 请任何人能帮助我。 I am new in this. 我是新来的。

Thanks 谢谢

I have finally found a solution and am sharing it just in case someone out there is in need of similar solution. 我终于找到了一个解决方案,并分享了它,以防万一有人需要类似的解决方案。

A was able to call-up a contact selector and instantly load the default phone number of a selected contact into my text box using a codava plugin, which is auto included on Telerik projects. 能够调用联系人选择器,并使用codava插件将选定联系人的默认电话号码立即加载到我的文本框中,该插件自动包含在Telerik项目中。

Firstly, i reference the codava within my HTML 首先,我在HTML中引用了codava

    <script src="cordova.js"></script>

Then i have this script to call the codava's pickContact plugin when the load contact button is pressed; 然后,当按下加载接触按钮时,我就有了此脚本来调用codava的pickContact插件;

    $('#myActionButton').click(function() {
        navigator.contacts.pickContact(function(contact) {
            var numberC = contact.phoneNumbers[0].value;
            $('#myTextBox').val(numberC);
       }, function(err) {
          alert(err);
        });
  });

More information on Codava plugins can be found at http://phonegap-plugins.com/ 有关Codava插件的更多信息,请访问http://phonegap-plugins.com/。

Hope this will be useful to someone else 希望这对其他人有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM