1

我想在联系人数据库中查询电话号码,如果所选联系人有多个号码,我会显示可用号码供用户选择其中一个。我有代码可以获取所选联系人的默认号码。

Cursor cursor = null;  
                String phoneNumber = "";  
                try 
                {  
                     Uri result = data.getData();          
                     // get the contact id from the Uri  
                     String id = result.getLastPathSegment();  

                     // query for everything email  
                     cursor = getContentResolver().query(Phone.CONTENT_URI,  
                             null, Phone.CONTACT_ID + "=?", new String[] { id },  
                             null);  

                     int pNumberIdx = cursor.getColumnIndex(Phone.NUMBER);  

                     // let's just get the first email  
                     if (cursor.moveToFirst()) 
                     {  
                         phoneNumber = cursor.getString(pNumberIdx);  
                     } 
                 } 
                catch (Exception e) 
                {  

                } 
                finally 
                {  
                     if (cursor != null) 
                     {  
                         cursor.close();  
                     }  
                     EditText emailEntry = (EditText) findViewById(R.id.pNumber);  
                     emailEntry.setText(phoneNumber);  
                     if (phoneNumber.length() == 0) 
                     {  
                         Toast.makeText(this, "No number found for contact.",  
                                 Toast.LENGTH_LONG).show();  
                     }  
                 }  
4

0 回答 0