마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

주소록 한사람당 여러번호 가져오기 소스 봐주세요.

0 추천

		TextView text = (TextView) findViewById(R.id.text);
		
		
		
		ContentResolver cr = getContentResolver();
		Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,"display_name"); 
		
		int ididx = cursor.getColumnIndex(ContactsContract.Contacts._ID);
		int nameidx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

		while (cursor.moveToNext()) {
			// 전화 번호는 서브 쿼리로 조사해야 함.
			String id = cursor.getString(ididx);
			Cursor cursor2 = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

			int numidx = cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
			// 전화의 타입에 따라 여러 개가 존재한다.
			ArrayList<String> strList = new ArrayList<String>(); 
			while (cursor2.moveToNext()) {		// 들어온 num이 같으면 하나로 인식해야 한다.
				String num = cursor2.getString(numidx).replace("-", "");
				if(num.length() >= 2 && num.trim().substring(0,2).equalsIgnoreCase("01")){
					if(!strList.contains(num)){
						strList.add(num);
						text.append(cursor.getString(nameidx)+" : "+strList.get(strList.size()-1)); 
						text.append("\n");
					}
				}
			}
			cursor2.close();
		
		}
		cursor.close();

현재 이렇게 코딩을 한 상태입니다.

그런데 친구가 별로 없으면 괜찮은데,

친구가 많으면 너무 느려지더라구요.

while문이 두개라서그런가....

어떻게 하는게 좋을까요 ㅠ?

슈퍼꽃붕어 (1,600 포인트) 님이 2014년 7월 4일 질문

2개의 답변

0 추천
main thread에서 시간이 오래 걸리는 작업을 하면 UI가 멈춰 보이거나 ANR이 발생합니다.

thread나 asyncTask같이 백그라운드로 쿼리 작업을 수행하고 완료시에 UI를 변경하면 됩니다.
익명사용자 님이 2014년 7월 4일 답변
스래드로 해도 여전히 느려용.ㅠㅠ
0 추천
그리고 ContactsProvier에서 일일이 쿼리를 하기보다 검색URI가 별도로 존재할겁니다.

저렇게 2중 loop문에 직접 비교 연산을 할 필요가 없습니다.
익명사용자 님이 2014년 7월 4일 답변
그걸 못찾겠네여 ....
...