저는 Contacts 앱이 있고 ContentObserver로 로컬 ORMLite 데이터베이스를 업데이트해야합니다. 주소록 업데이트를 확인해야합니다. 그렇다면 내 로컬 데이터베이스에 저장되지 않은 연락처 이름을 업데이트해야합니다.ContentObserver가 유창한 ORMLite 데이터베이스를 만듭니다
두 개의 항목 - CallItem (외부 연락처 필드가 있음)과 ContactItem이 있습니다.
내 ContentObserver :
class CallsContentObserver extends ContentObserver {
Context _context;
Handler _handler;
public CallsContentObserver(Handler handler, Context context) {
super(handler);
this._context = context;
this._handler = handler;
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
ArrayList<CallItem> contactsList = null;
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
Cursor contactLookupCursor = null;
try{
Dao<CallItem,Integer> daoSubject = db.getCallDao();
contactsList = (ArrayList<CallItem>) daoSubject.queryForAll();
for (CallItem contact : contactsList)
{
if (contact.getCall_contact() == null)
{
String contactNumber = Uri.encode(contact.getNumber());
String new_name = null;
contactLookupCursor =getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
while(contactLookupCursor.moveToNext()){
new_name = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
}
contactLookupCursor.close();
contact.setName(new_name);
daoSubject.update(contact);
}
}
}
catch(Exception e)
{e.printStackTrace();}
finally{db.close();}
}
}
그래서 내가 가진 모든 내 CallItems. 그런 다음 모든 CallItem에 ContactItem이 포함되어 있는지 확인합니다. 그렇지 않은 경우 해당 번호로 새 이름을 얻고이 CallItem을 DAO로 새로운 값으로 업데이트합니다. 하지만이 솔루션은 매우 느리므로 더 유창하게 만들고 싶습니다. 어떻게해야합니까?
당신은이 친구에게 어떤 해결책을 생각해 냈습니까? – Gray
내 솔루션을 추가했습니다 .. –