我在這裏回答我的問題,問題是由於在每個條目相同的URL許多有NotifyChange()請求,我使用下面的代碼延遲通知
第一個覆蓋了applyBatch功能
private boolean applyingBatch = false;
private List<Uri> delayedNotifications;
@NonNull
@Override
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
final SQLiteDatabase db = dbHelper.getWritableDatabase();
db.beginTransaction();
try {
applyingBatch = true;
final int numOperations = operations.size();
final ContentProviderResult[] results = new ContentProviderResult[numOperations];
for (int i = 0; i < numOperations; i++) {
results[i] = operations.get(i).apply(this, results, i);
}
db.setTransactionSuccessful();
applyingBatch = false;
/*
*
* Notify to all URL where change it made
* */
synchronized (delayedNotifications) {
for (Uri uri : delayedNotifications) {
getContext().getContentResolver().notifyChange(uri, null);
}
}
return results;
} finally {
applyingBatch = false;
db.endTransaction();
}
}
然後檢查批次作業的進度,然後存儲延遲發送,否則通知
protected void sendNotification(Uri uri) {
if (applyingBatch) {
if (delayedNotifications == null) {
delayedNotifications = new ArrayList<Uri>();
}
synchronized (delayedNotifications) {
if (!delayedNotifications.contains(uri)) {
delayedNotifications.add(uri);
LogUtil.debug("ProcessExeCheck added " + uri);
}
}
} else {
getContext().getContentResolver().notifyChange(uri, null);
}
}
檢查通知在插入,UPD通知吃掉和刪除
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri result;
long rawId;
switch (URI_MATCHER.match(uri)) {
case ALL_LIST:
rawId = dbHelper.getWritableDatabase().insert(Table.TABLE_NAME, null, values);
result = Station.getUriById(rawId);
default:
throw new IllegalArgumentException("Uri " + uri + " is not supported");
}
sendNotification(LIST.CONTENT_URI);
return result;
}
爲什麼AsyncTask沒有用? – Selvin
使用AsyncTask後,它仍然阻塞UI線程,並在某些設備上的應用程序得到停止響應 –
不可能...也許另一個操作在ContentProvider阻止它(例如:您運行此AsyncTask,然後(在結束之前)您查詢它) – Selvin