PhoneContact
private static final String[] phoneContact = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Photo.PHOTO_ID};
LoaderManager.getInstance(this).initLoader(0, null, new LoaderCallback());
private class LoaderCallback implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
Log.i(TAG, "onCreateLoader:" + Thread.currentThread().getName());
return new CursorLoader(
ContentProviderBasicActivity.this,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
phoneContact,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
readDataFromCursor(data);
Log.i(TAG, "onLoadFinished:" + Thread.currentThread().getName());
}
}
private void readDataFromCursor(Cursor cursor) {
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(
cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(
cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, name + " : " + number);
}
}
}
Cursor phoneCursor = resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
phoneContact,
ContactsContract.Contacts.DISPLAY_NAME+" = ?",
new String[]{"A Test"},
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
More LoaderManager
https://blog.csdn.net/u012514113/article/details/123949911
自定义
public static final String AUTHORITY = "com.example.demoprovider.provider";
private static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
public static final Uri PERSON_URI = Uri.withAppendedPath(AUTHORITY_URI,"man");
/**
*新增数据
*/
private void insert() {
ContentResolver resolver = this.getContentResolver();
Uri uri = DemoContract.PERSON_URI;
ContentValues contentValues = new ContentValues();
contentValues.put(DemoContract.Person.NAME,"tester");
contentValues.put(DemoContract.Person.PHONE,"12345678");
contentValues.put(DemoContract.Person.SEX,"男");
contentValues.put(DemoContract.Person.AGE,23);
resolver.insert(uri,contentValues);
}
private void delete() {
ContentResolver resolver = this.getContentResolver();
Uri uri = DemoContract.PERSON_URI;
int count = resolver.delete(uri,
DemoContract.Person.NAME+" = ?",
new String[]{"tester"});
Log.i(TAG, "delete count="+count);
}
private void update() {
ContentResolver resolver = this.getContentResolver();
Uri uri = DemoContract.PERSON_URI;
ContentValues contentValues = new ContentValues();
contentValues.put(DemoContract.Person.NAME,"nd");
contentValues.put(DemoContract.Person.PHONE,"28781771728");
int count = resolver.update(uri,contentValues,
DemoContract.Person.NAME+" = ?",
new String[]{"tester"});
Log.i(TAG, "update count="+count);
}
//query
ContentResolver resolver = this.getContentResolver();
Uri uri = DemoContract.PERSON_URI;
Cursor phoneCursor = resolver.query(
uri,
person,
null,
null,
null);
}
phoneCursor.close();