退出android系统中清除应用程序缓存 [英] Clear Application cache on exit in android
本文介绍了退出android系统中清除应用程序缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的就是清除应用程序退出应用程序的缓存。
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.
<应用程序 - >管理应用程序 - >我的应用程序 - >清除缓存>>
但我想通过在应用程序退出做这个任务..请帮我家伙..
but i wants to do this task by programming on exit of application.. please help me guys..
在此先感谢。
推荐答案
试试这个 -
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 -
- <一个href="http://stackoverflow.com/questions/4553238/how-to-clear-data-cache-of-the-application-through-$c$c">how-to-clear-data-cache-of-the-application-through-$c$c
这篇关于退出android系统中清除应用程序缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文