安卓:清除缓存编程
问题描述:
如何清除每个应用程序的缓存在我的Android手机programmmatically?不缓存清除编程是允许在Android的?如果允许的话,怎么办?我已经尝试研究它,我无法找到我需要的答案
How to clear the cache of every application in my android phone programmmatically? does the clearing of cache programmatically is allowed in android? if allowed, how? I already try to researched it and I can't find the answer that I need
答
我发现这一个:
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();
}
}
这也许对你有帮助。
It may be helpful to you.