ContentProviderClient release() 弃用

ContentProviderClient release() deprecated

我找到了一些删除数据库并使用 ContentProvider 重新创建它的解决方案。

        ContentResolver resolver = mContext.getContentResolver();
        ContentProviderClient client = resolver.acquireContentProviderClient(KOOPSContentProvider.AUTHORITY);

        assert client != null;
        KOOPSContentProvider provider = (KOOPSContentProvider) client.getLocalContentProvider();

        assert provider != null;
        provider.resetDatabase();

        client.release();

但是 ContentProviderClient class 有 release()deprecated, 还有其他方法吗释放资源。

已编辑: 如果我尝试使用 close(),它会显示如下警告。

This ContentProviderClient should be freed up after use with #release().

Many resources, such as TypedArrays, VelocityTrackers, etc., should be recycled (with a recycle() call) after use. This lint check looks for missing recycle() calls.

close()显示为禁用,为什么?

已被 close 取代,但仅在 API 24+

上可用

查看更多https://developer.android.com/reference/android/content/ContentProviderClient.html#close()

close on 24 等同于 release below 24 查看 ContentProviderClient

的源代码
  /**
 * Closes this client connection, indicating to the system that the
 * underlying {@link ContentProvider} is no longer needed.
 */
@Override
public void close() {
    closeInternal();
}

/**
 * @deprecated replaced by {@link #close()}.
 */
@Deprecated
public boolean release() {
    return closeInternal();
}

它被禁用因为你需要select更正api level

如果有人想知道代码应该是什么:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
    client.close();            
}
else
{
    client.release();
}