2011-11-09 17 views
2

私は自分のアプリ内で連絡先を開き、ユーザーが連絡先を選択できるようにしようとしています。その場合、ユーザーは使いたい電話番号を選択できます。今の私の方法では、適切な連絡先と電話番号を選択できますが、電話番号が自宅の電話番号か携帯電話番号かどうかをユーザーに伝える方法はわかりません。連絡先を開き、電話番号を取得する

それはどのようにセル、家庭、仕事であれば、私は次の数に言及しない、など

これは私が連絡先から電話番号を取得するには、今使っているものです。ありがとう! enter image description here

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

      try { 

       String id = contactData.getLastPathSegment(); 
       Cursor phoneCur = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = ?", new String[] { id }, null); 

       final ArrayList<String> phonesList = new ArrayList<String>(); 
       while (phoneCur.moveToNext()) { 
        // This would allow you get several phone addresses 
        // if the phone addresses were stored in an array 
        String phone = phoneCur 
          .getString(phoneCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
        phonesList.add(phone); 
       } 
       phoneCur.close(); 
       if (phonesList.size() == 0) { 
        Toast.makeText(this, "No Phone Number in Contact", 
          Toast.LENGTH_SHORT).show(); 
       } else if (phonesList.size() == 1) { 
        switch (requestCode) { 
        case (1): 
         edit1.setText(phonesList.get(0)); 
         break; 
        case (2): 
         edit2.setText(phonesList.get(0)); 
         break; 
        case (3): 
         edit3.setText(phonesList.get(0)); 
         break; 
        } 
       } else { 

        final String[] phonesArr = new String[phonesList.size()]; 
        for (int i = 0; i < phonesList.size(); i++) { 
         phonesArr[i] = phonesList.get(i); 
        } 

        AlertDialog.Builder dialog = new AlertDialog.Builder(
          Settings.this); 
        dialog.setTitle("Choose Phone Number"); 
        ((Builder) dialog).setItems(phonesArr, 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            String selectedEmail = phonesArr[which]; 
            switch (requestCode) { 
            case (1): 
             edit1.setText(selectedEmail); 
             break; 
            case (2): 
             edit2.setText(selectedEmail); 
             break; 
            case (3): 
             edit3.setText(selectedEmail); 
             break; 
            } 
           } 
          }).create(); 
        dialog.show(); 
       } 
      } catch (Exception e) { 
       Log.e("FILES", "Failed to get phone data", e); 
      } 
     } 
    } 

答えて

2
ContentResolver contentResolver = getContentResolver(); 

    Cursor cursor = contentResolver.query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
     new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, 
      ContactsContract.CommonDataKinds.Phone.TYPE }, 
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" 
      + contactId, null, null + " DESC"); 

    if (cursor.moveToFirst()) { 
     do { 
     int phoneNumberColumn = cursor.getColumnIndex(Phone.NUMBER); 
     int phoneTypecolumn = cursor.getColumnIndex(Phone.TYPE); 

     int phoneType = Integer.parseInt(cursor 
      .getString(phoneTypecolumn)); 

     String phoneNumberType = ""; 

     if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) { 
      phoneNumberType = "Home"; 
     } else if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) { 
      phoneNumberType = "Mobile"; 
     } .....//Similarly the other types could be obtained. 

     } while (cursor.moveToNext()); 
    } 

これは役立つだろうと思います。

関連する問題