【问题标题】:invoke contacts in app在应用程序中调用联系人
【发布时间】:2014-09-08 13:00:00
【问题描述】:

嗯,我正在开发我的应用程序,我想在我的应用程序中添加一个联系人列表......我试图理解某些东西,但是是的......但我什么都理解......

我知道我已经在清单中添加了权限

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

我也知道我必须创建一个意图......我不知道是为了什么......

请不要在这里重新发送这篇文章......我知道有一些有用的代码,但我不能聪明。有人可以帮我做一个简单的项目吗

How to call Android contacts list?

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    您可以有意地从电话簿中选择一个联系人:

     private void openPhonebook()
        {
            Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
            pickContactIntent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
            startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
        }
    

    您应该在onActivityResult() 中处理响应意图:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent intent)
        {
            super.onActivityResult(requestCode, resultCode, intent);
    
            if ( requestCode == PICK_CONTACT_REQUEST )
            {
                if ( resultCode == RESULT_OK )
                {
                    Uri contactUri = intent.getData();
                    String[] projection = { Phone.NUMBER, Phone.DISPLAY_NAME };
    
                    Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
                    cursor.moveToFirst();
    
                    int column = cursor.getColumnIndex(Phone.NUMBER);
                    String phone = cursor.getString(column);
    
                    column = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                    String name = cursor.getString(column);
    
                    //set phone and to EditText
                    etName.setText(name.trim());
                    petPhone.setText(phone);
    
                    cursor.close();
                }
            }
        }
    

    还有权限:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多