2

I found some solution to delete database and recreate it using 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();

but in that ContentProviderClient class has release() which is deprecated, Is there any other way to free up resources.

Edited: If I try to use close(), It is displaying as warning as follow.

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.

and close() displaying as disabled, why?

enter image description here

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

2 Answers2

5

in case anyone is wondering what the code should be:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
    client.close();            
}
else
{
    client.release();
}
Wirling
  • 4,810
  • 3
  • 48
  • 78
2

it is replaced by close but it is available only on API 24+

see more https://developer.android.com/reference/android/content/ContentProviderClient.html#close()

close on 24 is the same as release below 24 see source code of 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();
}

it is disable because you need to select correct api level enter image description here

WenChao
  • 3,586
  • 6
  • 32
  • 46
  • then What about older device? – Pratik Butani Feb 16 '17 at 04:37
  • `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. *Displaying as warning.* – Pratik Butani Feb 16 '17 at 04:45
  • I guess the `lint` warning is a bit misleading :) – WenChao Feb 16 '17 at 04:48
  • use `release` on API 24 below, use a check using `Build.VERSION.SDK_INT` like mentioned here https://developer.android.com/training/basics/supporting-devices/platforms.html#version-codes – WenChao Feb 16 '17 at 04:52