블로그 이미지

NCFPTeam's Blog

세상을 향한 통로... by nineclouds


[Android] Content Provider

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
    null, null, null, null); 
  while (cursor.moveToNext()) { 
   String contactId = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts._ID)); 
   String hasPhone = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
   if (Boolean.parseBoolean(hasPhone)) { 
                // You know have the number so now query it like this
    Cursor phones = getContentResolver().query( 
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      null, 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
      null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString( 
       phones.getColumnIndex( 
         ContactsContract.CommonDataKinds.Phone.NUMBER));                 
    } 
    phones.close(); 
   } 
   Cursor emails = getContentResolver().query( 
     ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
     null, 
     ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, 
     null, null); 
   while (emails.moveToNext()) { 
                // This would allow you get several email addresses 
    String emailAddress = emails.getString( 
      emails.getColumnIndex( 
        ContactsContract.CommonDataKinds.CommonDataColumns.DATA)); 
   } 
   emails.close(); 
  } 
  cursor.close();
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while(people.moveToNext()) {
 int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
     String contact = people.getString(nameFieldColumnIndex);
 int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
     String number = people.getString(nameFieldColumnIndex);
}

people.close();
private String getNote(long contactId) { 
  String note = null; 
  String[] columns = new String[] 
                                   { ContactsContract.CommonDataKinds.Note.NOTE }; 
  String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
  String[] whereParameters = new String[]{Long.toString(contactId), 
    ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
  Cursor contacts = getContentResolver().query 
  (ContactsContract.Data.CONTENT_URI, projection, where, 
    whereParameters, null); 
  if (contacts.moveToFirst()) { 
   rv = contacts.getString(0); 
  } 
  contacts.close(); 
  return note; 
 }
package com.ncfp.rataplan;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;

public class ContactsView extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView contactView = (TextView) findViewById(R.id.contactview);

		Cursor cursor = getContacts();

		while (cursor.moveToNext()) {
			String displayId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
			String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
			contactView.append("Name: ");
			//contactView.append(displayId);
			contactView.append(displayName);
			contactView.append(hasPhone);
			//contactView.append("\n");
			
			if(!Boolean.parseBoolean(hasPhone)) {
				contactView.append("_");

				Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
						null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+ displayId, null, null);
				while(phones.moveToNext()) {
					String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					contactView.append(phoneNumber);

					contactView.append("\n");
			
				}
			
			}
		}
	}

	private Cursor getContacts() {
		// Run query
		Uri uri = ContactsContract.Contacts.CONTENT_URI;
		String[] projection = new String[] { ContactsContract.Contacts._ID,
				ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER };
		String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
				+ ("1") + "'";
		String[] selectionArgs = null;
		String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
				+ " COLLATE LOCALIZED ASC";

		//return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
		return managedQuery(uri, null, null, null, null);
	}

}
Top