1

我有文本视图。单击它会打开本地联系人列表。一旦用户选择了一个联系人,我应该在我的应用程序中显示该号码。我可以显示名称但无法显示数字。请帮忙。

提前致谢。

这是我的代码,但在选择联系人后,我的应用程序崩溃了。“不幸的是,'app_name' 已停止”

  public void dail(View v) 
   {
     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  Contacts.CONTENT_URI);  
     startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
   }    


  protected void onActivityResult(int requestCode, int resultCode, Intent data) 
   {  
  if (data != null) {
        Uri uri = data.getData();

        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver().query(uri, new String[]{ 
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    String number = c.getString(0);
                    int type = c.getInt(1);
                    showSelectedNumber(type, number);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
 }

 public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();      
 }
}
4

3 回答 3

3

在你的 onActivity 结果中试试这个。它会起作用的。

 ContentResolver cr = getContentResolver();
    cursor = cr.query(intent.getData(), null, null, null, null);

    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
        if (Integer
                .parseInt(cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phones.close();
        } else {
            snipp.showAlertDialog(getApplicationContext(), "No Number",
                    "Cannot read number", false);
        }

    }
    cursor.close();
于 2013-10-26T12:17:21.220 回答
0

在你的下面试试onActivityResult

ContentResolver cr = getContentResolver();

Uri contactData = data.getData();
Log.v("Contact", contactData.toString());
Cursor c = managedQuery(contactData,null,null,null,null);

if(c.moveToFirst()){
        id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
    Log.v("Contact", "ID: " + id.toString());
        name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    Log.v("Contact", "Name: " + name.toString());

    if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);

      while(pCur.moveToNext()){
          phone = pCur.getString(pCur .getColumnIndex(Phone.NUMBER));
          Log.v("getting phone number", "Phone Number: " + phone);
      }
  }

}
于 2013-10-26T12:18:52.643 回答
0

尝试这个

将此代码放在您的文本视图中OnclickListner

Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

在活动结果中输入此代码

@Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
            super.onActivityResult(reqCode, resultCode, data);
            switch (reqCode) {
                    case (PICK_CONTACT):
                            if (resultCode == Activity.RESULT_OK) {
                                    Uri contactData = data.getData();
                                    Cursor c = managedQuery(contactData, null, null, null, null);
                                    if (c.moveToFirst()) {
                                            String name = c.getString(c.getColumnIndexOrThrow(People.NAME))+" : "+c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
                                            //
                                            txtContacts1.setText(name);
                                    }
                            }
                            break;
            }

对于没有放此代码的日常

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getInt(c.getColumnIndexOrThrow(People.NUMBER)));
context.startActivity(intent);
于 2013-10-26T12:26:53.853 回答