在android中退出时清除应用程序缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10977288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear Application cache on exit in android
提问by Arun Joshi
What I want to do is to clear the cache memory of application on exit of application.
我想要做的是在应用程序退出时清除应用程序的缓存。
this task i can do manually by this steps.
这个任务我可以通过这个步骤手动完成。
< Apps --> Manage Apps --> "My App" --> Clear Cache>>
<应用程序-->管理应用程序-->“我的应用程序”-->清除缓存>>
but i wants to do this task by programming on exit of application.. please help me guys..
但我想通过在退出应用程序时编程来完成这项任务.. 请帮帮我..
Thanks in advance..
提前致谢..
回答by Praveenkumar
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 -
请参阅这些链接 -
回答by Md Abdul Gafur
To clear Application Data Please Try this way. I think it help you.
要清除应用程序数据请尝试这种方式。我认为对你有帮助。
public void clearApplicationData()
{
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
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();
}
回答by Rod
Just a clarification, the answers work properly except you have to pass the Application context to trimCacheinstead of the Activity context (to avoid memory leak), since trimCacheis an static method.
只是澄清一下,答案正常工作,除非您必须将 Application 上下文传递给trimCache而不是 Activity 上下文(以避免内存泄漏),因为trimCache是一个静态方法。
@Override
protected void onDestroy() {
super.onDestroy();
try {
trimCache(getApplicationContext()); //if trimCache is static
} catch (Exception e) {
e.printStackTrace();
}
}
However, otherwise, you can make trimCachenon-static and there is no need to pass any Context.
但是,否则,您可以使trimCache非静态并且不需要传递任何上下文。
public void trimCache() {
try {
File dir = getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}