所以我想要做的是在我更改联系人时在TextView中显示所有联系人我希望我的应用程序重新加载包括新联系人在内的联系人。出于某种原因,即使我创建新的联系人
之后也永远不会调用onChange()
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputText = (TextView) findViewById(R.id.textView1);
outputText.setMaxLines(Integer.MAX_VALUE);
outputText.setMovementMethod(new ScrollingMovementMethod());
this.getApplicationContext().getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contentObserver);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if(settings.getBoolean("first", true)){
fetchContacts();
} else {
outputText.setText(settings.getString("contacts", "no contacts detected"));
}
}
public void fetchContacts(){
// gathers all the contacts
}
public void onPause(){
super.onPause();
avePreference();
}
public void savePreference(){
//code for setting shared preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("contacts",outputText.getText()+"");
editor.putBoolean("first", false);
editor.commit();
}
private class ContactObserver extends ContentObserver {
public ContactObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
fetchContacts();
}
@Override
public boolean deliverSelfNotifications() {
Log.e("","~~~~~~ Change");
return true;
}
}