Clear Cache in Android Application programmatically
If you are looking for delete cache of your own application then simply delete your cache directory and its all done !
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
Clearing Cache in android App Programmatically
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle *) {
super.onCreate(*);
setContentView(R.layout.main);
}
@Override
protected void onStop(){
super.onStop();
}
//Fires after the OnStop() state
@Override
protected void onDestroy() {
super.onDestroy();
try {
trimCache(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
}
else {
return false;
}
}
Is there any way to Clear all Cache files programmatically?
This intent should be launched using
Activity#startActivityForResult(Intent, int) so that the user knows
which app is requesting to clear cache.
Check the documentation.
Use "startActivityForResult" instead of "startActivity":
//Get cache dialog
int requestCode = 999;
Intent intent = new Intent(StorageManager.ACTION_CLEAR_APP_CACHE);
activity.startActivityForResult(intent, requestCode);
How do I delete my app cache when the app is exited?
If you are looking for delete cache of your own application then simply delete your cache directory and it's all done!
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
Clear Application cache on exit in android
Try this one -
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle *) {
super.onCreate(*);
setContentView(R.layout.main);
}
@Override
protected void onStop(){
super.onStop();
}
//Fires after the OnStop() state
@Override
protected void onDestroy() {
super.onDestroy();
try {
trimCache(this);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
}
Refer these links -
- how-to-clear-data-cache-of-the-application-through-code
Related Topics
How to Fix Attempt to Invoke Interface Method on a Null Object Reference
How to Start Android Application on Device Boot
How to Show the Number Keyboard on an Edittext in Android
Decrease Image Size Without Losing Its Quality in Android
Flutter:Renderbox Was Not Laid Out: Renderrepaintboundary#58C65 Relayoutboundary=Up1 Needs-Paint
Java.Lang.Runtimeexception: Canvas: Trying to Draw Too Large(144000000Bytes) Bitmap
How to Reduce an Image File Size Before Uploading to a Server
How to Know Location Area Code and Cell Id in Android Phone
Clear Cache in Android Application Programmatically
How to Get the Height of Recyclerview Item in "Onbindviewholder"
How to Change Listview Height Dynamically in Android
Changing Background Color of the Layout on a Button Click in Android
How to Get Device Id in Flutter of Both Android and Ios
Textview With Too Long Strings
React-Native: Module Appregistry Is Not a Registered Callable Module
How to Center Items of a Recyclerview
Install Apps Silently, With Granted Install_Packages Permission