Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Script:
import android.content.Context;
import android.os.AsyncTask;
import java.io.File;
public class LimparCacheTask extends AsyncTask<Void, Void, Boolean> {
private Context context;
public LimparCacheTask(Context context) {
this.context = context;
}
@Override
protected Boolean doInBackground(Void... voids) {
try {
File cacheDir = context.getCacheDir();
return deleteDir(cacheDir);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (String child : children) {
boolean success = deleteDir(new File(dir, child));
if (!success) {
return false;
}
}
}
return dir.delete();
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
System.out.println("Cache limpo com sucesso!");
} else {
System.out.println("Falha ao limpar o cache.");
}
}
}
Como Executar o Script:
LimparCacheTask
passando o contexto atual:
new LimparCacheTask(this).execute();