주소록 읽기

통화 녹음을 할 것을 준비하는데, 다음은 비동기적으로 통신록의 코드를 조회하는 것이다

	private static final String TAG = "ContractChoseActivity";
	
	private int sdkVersion = 4;
	private String mColumnName = null;

	
	private ListView mListView;
	
	private MyAdapter mAdapter;
	private ListQueryHandler mQueryHandler;
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		
		setContentView(R.layout.contract);
		mListView = (ListView) this.findViewById(R.id.contract_list);
		
		mQueryHandler = new ListQueryHandler(getContentResolver());
		queryList(mQueryHandler);
	}
	
	
	private void queryList(AsyncQueryHandler async) {
		if (async != null) {
			
			try {
				sdkVersion = Integer.parseInt(android.os.Build.VERSION.SDK.trim());
			} catch (Exception e) {
				sdkVersion = 3;
			}
			
            Uri personUri = null;
            if(sdkVersion > 4){//2.x,sdk 
                personUri = Uri.parse("content://com.android.contacts/contacts");
                mColumnName = "display_name";
            }else{//1.6 SDK
                personUri = Contacts.People.CONTENT_URI;
                mColumnName = Contacts.People.NAME;
            }
            // 
			async.startQuery(0, null, personUri, null, null, null, null);
		}
	}
	
	
	class MyAdapter extends CursorAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context, Cursor c) {
			super(context, c);
			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getListLength() {
			int ret = 0;
			if (getCursor() != null)
				ret = getCount();
			return ret;

		}

		
		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ItemStruct struct = (ItemStruct) view.getTag();
			
			if(cursor != null){
				try{
					struct.name.setText(cursor.getString(cursor.getColumnIndex(mColumnName)));
//					struct.number.setText(cursor.getString((cursor.getColumnIndex(Contacts.People.NUMBER))));
				}catch(Exception ex){
					Log.d(TAG, "exception>>"+ex.toString());
					ex.printStackTrace();
				}

			}
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View convertView = null;
			convertView = mInflater.inflate(R.layout.contract_row, parent, false);
			ItemStruct struct = new ItemStruct();
			struct.checkbox = (CheckBox) convertView.findViewById(R.id.contract_checkbox);
			struct.name = (TextView) convertView.findViewById(R.id.contract_name);
			struct.number = (TextView) convertView.findViewById(R.id.contract_number);
			convertView.setTag(struct);
			return convertView;
		}

		private class ItemStruct {
			CheckBox checkbox;
			TextView name;
			TextView number;
		}

	}
	
	class ListQueryHandler extends AsyncQueryHandler {

		public ListQueryHandler(ContentResolver cr) {
			super(cr);
		}

		/**
		 *  
		 */
		public void onQueryComplete(int token, Object cookie, Cursor cursor) {

			if (cursor != null) {
				
				if (mAdapter == null) {
					mAdapter = new MyAdapter(ContractChoseActivity.this, cursor);
					mListView.setAdapter(mAdapter);
				} else {
					mAdapter.changeCursor(cursor);

				}
			}

		}

	}
	
	

원래 전화번호까지 표시하려고 했는데 같은 표에 없어요. 구글의 주소록은 간단한 전화번호부가 아니에요. 이 표에는 has_phone_number, 그리고 id를 계속 따라가서 다른 표를 조회해야만 모든 전화번호를 얻을 수 있습니다.다음 코드에는 다음과 같은 방법이 있습니다.
참조:http://topic.csdn.net/u/20101124/12/45aa763f-82ed-4c23-9877-64d725c6aed9.html
  1. Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);       
    2. while (cursor.moveToNext())     
    3. {        
    4.     String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));    
    5.     String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));    
    6.     if (Boolean.parseBoolean(hasPhone))     
    7.     {    
    8.         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);         
    9.         while (phones.moveToNext())     
   10.         {          
   11.             String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));        
   12.         }         
   13.         phones.close();        
   14.     }        
   15.     Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);    
   16.     while (emails.moveToNext())     
   17.     {                     
   18.         String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.CommonDataColumns.DATA));    
   19.     }        
   20.     emails.close();       
   21. }       
   22. cursor.close();   
 

좋은 웹페이지 즐겨찾기