Android獲取聯絡人和電話號碼

神棍先生發表於2017-11-08

獲取聯絡人和電話號碼

  private void queryContactPhoneNumber() {
        String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                cols, null, null, null);
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            // 取得聯絡人名字
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String name = cursor.getString(nameFieldColumnIndex);
            String number = cursor.getString(numberFieldColumnIndex);
            Toast.makeText(this, name + " " + number, Toast.LENGTH_SHORT).show();
        }
    }

 根據電話號碼查詢聯絡人姓名的方法:

public class AndroidTest extends Activity {   
    private static final String TAG = "AndroidTest";   
    
    private TextView m_TextView;   
    private EditText m_EditText;   
        
    private String mNumber;   
        
    @Override  
    public void onCreate(Bundle savedInstanceState){   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
            
        m_TextView = (TextView) findViewById(R.id.TextView01);   
        m_EditText = (EditText) findViewById(R.id.EditText01);   
    
        m_TextView.setTextSize(20);   
        /**  
        * 設定當m_EditText中為空時提示的內容    
        * 在XML中同樣可以實現:android:hint="請輸入賬號"  
        */  
        m_EditText.setHint("請輸入賬號");   
    
        /* 設定EditText事件監聽 */  
        m_EditText.setOnKeyListener(new EditText.OnKeyListener() {   
        public boolean onKey(View arg0, int arg1, KeyEvent arg2)   
        {   
            // TODO Auto-generated method stub   
            // 得到文字,將其顯示到TextView中   
//          m_TextView.setText("文字框中內容是:" + m_EditText.getText().toString());   
            return false;   
        }   
        });   
            
        m_EditText.setOnFocusChangeListener(new OnFocusChangeListener() {   
    
            public void onFocusChange(View v, boolean hasFocus) {   
                // TODO Auto-generated method stub   
                mNumber = ((EditText)v).getText().toString();   
                Log.d(TAG, "mNumber = " + mNumber);   
                getPeople();   
            }   
                
        });   
            
        m_EditText.addTextChangedListener(new TextWatcher() {   
    
            public void afterTextChanged(Editable s) {   
                // TODO Auto-generated method stub   
                    
            }   
    
            public void beforeTextChanged(CharSequence s, int start, int count,   
                    int after) {   
                // TODO Auto-generated method stub   
                    
            }   
    
            public void onTextChanged(CharSequence s, int start, int before,   
                    int count) {   
                // TODO Auto-generated method stub   
                // 得到文字,將其顯示到TextView中   
                m_TextView.setText("文字框中內容是:" + m_EditText.getText().toString());   
            }   
                
        });   
    }   
        
    /*  
     * 根據電話號碼取得聯絡人姓名  
     */  
    public void getPeople() {   
        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,   
                                ContactsContract.CommonDataKinds.Phone.NUMBER};   
    
        Log.d(TAG, "getPeople ---------");   
            
        // 將自己新增到 msPeers 中   
        Cursor cursor = this.getContentResolver().query(   
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
                projection,    // Which columns to return.   
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = `" + mNumber + "`", // WHERE clause.   
                null,          // WHERE clause value substitution   
                null);   // Sort order.   
    
        if( cursor == null ) {   
            Log.d(TAG, "getPeople null");   
            return;   
        }   
        Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount());   
        for( int i = 0; i < cursor.getCount(); i++ )   
        {   
            cursor.moveToPosition(i);   
                
            // 取得聯絡人名字   
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);      
            String name = cursor.getString(nameFieldColumnIndex);   
            Log.i("Contacts", "" + name + " .... " + nameFieldColumnIndex); // 這裡提示 force close   
            m_TextView.setText("聯絡人姓名:" + name);   
        }   
    }   
        
}  


相關文章