下面列出了android.content.ContentResolver#unregisterContentObserver ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setRegistered(boolean registered) {
if (mRegistered == registered) return;
final ContentResolver cr = mSystemContext.getContentResolver();
final int userId = mUserContext.getUserId();
if (mRegistered) {
if (DEBUG) Log.d(TAG, "unregister content observer u=" + userId);
cr.unregisterContentObserver(mObserver);
}
mRegistered = registered;
if (DEBUG) Log.d(TAG, "mRegistered = " + registered + " u=" + userId);
if (mRegistered) {
if (DEBUG) Log.d(TAG, "register content observer u=" + userId);
cr.registerContentObserver(Instances.CONTENT_URI, true, mObserver, userId);
cr.registerContentObserver(Events.CONTENT_URI, true, mObserver, userId);
cr.registerContentObserver(Calendars.CONTENT_URI, true, mObserver, userId);
}
}
private void registerBrightObserver() {
try {
if (mBrightObserver != null) {
if (!isRegistered) {
final ContentResolver cr = getContentResolver();
cr.unregisterContentObserver(mBrightObserver);
cr.registerContentObserver(BRIGHTNESS_MODE_URI, false, mBrightObserver);
cr.registerContentObserver(BRIGHTNESS_URI, false, mBrightObserver);
cr.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightObserver);
isRegistered = true;
}
}
} catch (Throwable throwable) {
LogUtils.e(TAG, "register mBrightObserver error! " + throwable);
}
}
/**
* Call from Application.onTerminate(), which is not guaranteed to ever be called.
*/
public void onTerminate() {
sContext.unregisterReceiver(mModel);
final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
launcherApps.removeOnAppsChangedCallback(mModel);
PackageInstallerCompat.getInstance(sContext).onStop();
ContentResolver resolver = sContext.getContentResolver();
resolver.unregisterContentObserver(mFavoritesObserver);
}
private void unregisterContentObservers() {
ContentResolver cr = getContentResolver();
if (mTaskContentObserver != null) { // just paranoia
cr.unregisterContentObserver(mTaskContentObserver);
mTaskContentObserver = null;
handler = null;
}
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it prior to this method being run.
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it when this method is run
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
private void unregisterObserver() {
if (mFormatChangeObserver != null) {
final ContentResolver resolver = getContext().getContentResolver();
resolver.unregisterContentObserver(mFormatChangeObserver);
}
}
@Override
public void onClose() {
final ContentResolver cres = mService.getContentResolver();
cres.unregisterContentObserver(mObserver);
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it prior to this method being run.
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
@Override
public void onClose() {
final ContentResolver cres = mService.getContentResolver();
cres.unregisterContentObserver(mObserver);
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it prior to this method being run.
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it prior to this method being run.
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
/**
* Tests inserting a single row of data via a ContentResolver
*/
@Test
public void testInsert() {
/* Create values to insert */
ContentValues testTaskValues = new ContentValues();
testTaskValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, "Test description");
testTaskValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, 1);
/* TestContentObserver allows us to test if notifyChange was called appropriately */
TestUtilities.TestContentObserver taskObserver = TestUtilities.getTestContentObserver();
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (tasks) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
TaskContract.TaskEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
taskObserver);
Uri uri = contentResolver.insert(TaskContract.TaskEntry.CONTENT_URI, testTaskValues);
Uri expectedUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, 1);
String insertProviderFailed = "Unable to insert item through Provider";
assertEquals(insertProviderFailed, uri, expectedUri);
/*
* If this fails, it's likely you didn't call notifyChange in your insert method from
* your ContentProvider.
*/
taskObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(taskObserver);
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it prior to this method being run.
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
/**
* Tests deleting a single row of data via a ContentResolver
*/
@Test
public void testDelete() {
/* Access writable database */
TaskDbHelper helper = new TaskDbHelper(InstrumentationRegistry.getTargetContext());
SQLiteDatabase database = helper.getWritableDatabase();
/* Create a new row of task data */
ContentValues testTaskValues = new ContentValues();
testTaskValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, "Test description");
testTaskValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, 1);
/* Insert ContentValues into database and get a row ID back */
long taskRowId = database.insert(
/* Table to insert values into */
TaskContract.TaskEntry.TABLE_NAME,
null,
/* Values to insert into table */
testTaskValues);
/* Always close the database when you're through with it */
database.close();
String insertFailed = "Unable to insert into the database";
assertTrue(insertFailed, taskRowId != -1);
/* TestContentObserver allows us to test if notifyChange was called appropriately */
TestUtilities.TestContentObserver taskObserver = TestUtilities.getTestContentObserver();
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (tasks) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
TaskContract.TaskEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
taskObserver);
/* The delete method deletes the previously inserted row with id = 1 */
Uri uriToDelete = TaskContract.TaskEntry.CONTENT_URI.buildUpon().appendPath("1").build();
int tasksDeleted = contentResolver.delete(uriToDelete, null, null);
String deleteFailed = "Unable to delete item in the database";
assertTrue(deleteFailed, tasksDeleted != 0);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
taskObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(taskObserver);
}
/**
* This test deletes all records from the weather table using the ContentProvider. It also
* verifies that registered ContentObservers receive onChange callbacks when data is deleted.
* <p>
* It finally queries the ContentProvider to make sure that the table has been successfully
* cleared.
* <p>
* NOTE: This does not delete the table itself. It just deletes the rows of data contained
* within the table.
* <p>
* Potential causes for failure:
* <p>
* 1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
* getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
* <p>
* 2) The cursor returned from the query was null
* <p>
* 3) After the attempted deletion, the ContentProvider still provided weather data
*/
@Test
public void testDeleteAllRecordsFromProvider() {
/*
* Ensure there are records to delete from the database. Due to our setUp method, the
* database will not have any records in it prior to this method being run.
*/
testBulkInsert();
/*
* TestContentObserver allows us to test weather or not notifyChange was called
* appropriately. We will use that here to make sure that notifyChange is called when a
* deletion occurs.
*/
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
/*
* A ContentResolver provides us access to the content model. We can use it to perform
* deletions and queries at our CONTENT_URI
*/
ContentResolver contentResolver = mContext.getContentResolver();
/* Register a content observer to be notified of changes to data at a given URI (weather) */
contentResolver.registerContentObserver(
/* URI that we would like to observe changes to */
WeatherContract.WeatherEntry.CONTENT_URI,
/* Whether or not to notify us if descendants of this URI change */
true,
/* The observer to register (that will receive notifyChange callbacks) */
weatherObserver);
/* Delete all of the rows of data from the weather table */
contentResolver.delete(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null);
/* Perform a query of the data that we've just deleted. This should be empty. */
Cursor shouldBeEmptyCursor = contentResolver.query(
WeatherContract.WeatherEntry.CONTENT_URI,
/* Columns; leaving this null returns every column in the table */
null,
/* Optional specification for columns in the "where" clause above */
null,
/* Values for "where" clause */
null,
/* Sort order to return in Cursor */
null);
/*
* If this fails, it's likely you didn't call notifyChange in your delete method from
* your ContentProvider.
*/
weatherObserver.waitForNotificationOrFail();
/*
* waitForNotificationOrFail is synchronous, so after that call, we are done observing
* changes to content and should therefore unregister this observer.
*/
contentResolver.unregisterContentObserver(weatherObserver);
/* In some cases, the cursor can be null. That's actually a failure case here. */
String cursorWasNull = "Cursor was null.";
assertNotNull(cursorWasNull, shouldBeEmptyCursor);
/* If the count of the cursor is not zero, all records weren't deleted */
String allRecordsWereNotDeleted =
"Error: All records were not deleted from weather table during delete";
assertEquals(allRecordsWereNotDeleted,
0,
shouldBeEmptyCursor.getCount());
/* Always close your cursor */
shouldBeEmptyCursor.close();
}
void unobserve() {
ContentResolver resolver = mContext.getContentResolver();
resolver.unregisterContentObserver(this);
}
@Override
public void onClose() {
final ContentResolver cres = mService.getContentResolver();
cres.unregisterContentObserver(mObserver);
}
public void unregister(Context context) {
ContentResolver resolver = context.getContentResolver();
resolver.unregisterContentObserver(this);
}
static void removeContentObserver(Activity activity) {
ContentResolver contentResolver = activity.getContentResolver();
contentResolver.unregisterContentObserver(new ContactsObserver(new Handler(), activity));
}