prsandroid prsandroid - 1 month ago 20
Android Question

How to listening contact changes in the phone book like adding a new contact,updating,deleting

public class MainActivity extends AppCompatActivity {
ArrayList<SelectUser> selectUsers;
List<SelectUser> temp;
ListView listView;
Cursor phones, email;
public static final String INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED = "finishActivityOnSaveCompleted";
ContentResolver resolver;
int INSERT_CONTACT_REQUEST = 2;
SelectUserAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

selectUsers = new ArrayList<SelectUser>();
resolver = this.getContentResolver();
listView = (ListView) findViewById(R.id.contacts_list);

phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
LoadContact loadContact = new LoadContact();
loadContact.execute();
TextView textView =(TextView)findViewById(R.id.AddHint);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent i = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);

i.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, true);
startActivityForResult(i, INSERT_CONTACT_REQUEST);
}
});
}

// Load data on background
class LoadContact extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();

}

@Override
protected Void doInBackground(Void... voids) {
// Get Contact list from Phone

if (phones != null) {
Log.e("count", "" + phones.getCount());
if (phones.getCount() == 0) {
Toast.makeText(MainActivity.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
}

while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String EmailAddr = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA2));

SelectUser selectUser = new SelectUser();
selectUser.setName(name);
selectUser.setPhone(phoneNumber);
selectUser.setEmail(id);
selectUsers.add(selectUser);
}
} else {
Log.e("Cursor close 1", "----------------");
}
//phones.close();
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter = new SelectUserAdapter(selectUsers, MainActivity.this);
listView.setAdapter(adapter);

// Select item on listclick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Log.e("search", "here---------------- listener");

SelectUser data = selectUsers.get(i);
}
});

listView.setFastScrollEnabled(true);
}
}

@Override
protected void onStop() {
super.onStop();
phones.close();
}
}


How to listening contact changes in the phone book like adding a new contact,updating,deleting.How do I make my Android ContentObserver for ContactsContract detect a added, updated or deleted contact?

Answer

Use ContentResolver

class MyObserver extends ContentObserver {
// left blank below constructor for this Contact observer example to work
// or if you want to make this work using Handler then change below registering  //line      
public MyObserver(Handler handler) {
  super(handler);           
}

@Override
public void onChange(boolean selfChange) {
  this.onChange(selfChange, null);
  Log.e("", "~~~~~~" + selfChange);
   // Override this method to listen to any changes
}        

@Override
public void onChange(boolean selfChange, Uri uri) {
  //On Contact add/delete this method is fired
}        
}

Register Observer like this

getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new MyObserver());

Tutorial available here