我是android开发的新手,现在我完全被卡住了!
所以我希望你能帮助我!

所以-我正在尝试从联系人选择器中获取联系人的电话号码和电子邮件地址。
但是当我显示输出时,它说是0和null。
我究竟做错了什么?
这是代码!

if (view == btnPhonebook2) {
        Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);
    }

  public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    switch (reqCode) {
    case (PICK_CONTACT):
        if (resultCode == Activity.RESULT_OK) {
            // String id = null;
            // FIRST TRY

              Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null);
              if(c.moveToFirst()) {
                  String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
                  int number = c.getInt(c.getColumnIndexOrThrow(People.PRIMARY_PHONE_ID));
                  String email = c.getString(c.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));


              //+" : c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
              Toast.makeText(getApplicationContext(),"you selected : "+name+"\n With the telephone number :"+number+"\n And the email :"+email,Toast.LENGTH_LONG).show();

              DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                        case DialogInterface.BUTTON_POSITIVE:
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "60158096"));
                              startActivity(intent);
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            Intent email = new Intent(Intent.ACTION_SEND);
                              email.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                              email.putExtra(Intent.EXTRA_SUBJECT, "subject");
                              email.putExtra(Intent.EXTRA_TEXT, "message");
                              email.setType("message/rfc822");
                              startActivity(Intent.createChooser(email, "Choose an Email client :"));
                            break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("How will you contact: "+name).setPositiveButton("Call", dialogClickListener)
                    .setNegativeButton("Email", dialogClickListener).show();


当我显示字符串名称时-很好!!

最佳答案

我在here之前回答了类似的问题。

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
if (cur.getCount() > 0) {
  while (cur.moveToNext()) {
    String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
    if (Integer .parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
    {
      Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
      while (pCur.moveToNext()) {
        phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        // This was the phone number for that contact id...
      }
    pCur.close();
    Cursor emailCur = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
    while (emailCur.moveToNext()) {
    // For Email
      emailContact = emailCur .getString(emailCur .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
      emailType = emailCur .getString(emailCur .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
    }
  }
}


同样,您可以获取image uri

代码的问题在这里:

String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
int number = c.getInt(c.getColumnIndexOrThrow(People.PRIMARY_PHONE_ID));
String email = c.getString(c.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));


使用字符串类型而不是int和Contacts.People class is deprecated from api 5。另外,People.PRIMARY_PHONE_ID会给您ID,而不是PhoneNumber。

String id = c.getString(c.getColumnIndex(People.PRIMARY_PHONE_ID));

关于java - 设备的电话号码返回0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26913315/

10-10 12:44