Java 无法以编程方式清除缓存内存
我已尝试使用下面的代码通过应用程序中的按钮清除缓存。Java 无法以编程方式清除缓存内存,java,android,Java,Android,我已尝试使用下面的代码通过应用程序中的按钮清除缓存。activity\u main.xml <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParen
activity\u main.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Cache"
android:id="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrive Data"
android:id="@+id/button3" />
</LinearLayout>
我错过了什么?onStop()和onDestroy()方法包含在您的活动中。您的活动onDestroy()添加以下代码
@Override
protected void onStop(){
super.onStop();
}
// 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();
}
公共静态缓存(上下文){
试一试{
File dir=context.getCacheDir();
if(dir!=null&&dir.isDirectory()){
deleteDir(dir);
}
}捕获(例外e){
//TODO:处理异常
}
}
公共静态布尔deleteDir(文件目录){
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
我想在单击按钮时清除缓存。我替换了您的trimCache()和dleteDir()。仍然没有改进你在你的活动中包括onDestroy()吗?我说onDestroy()是必需的!要在onClick()中调用trimCache()?但我不想关闭我的活动。在此过程中,您的活动也会在缓存中。。。如果不关闭“活动”意味着如何清除缓存?明白我的意思吗?谢谢你的解释。我包括了onDestroy()
,但仍然没有改进。再次打开“活动”时,它将重新获取数据。
@Override
protected void onStop(){
super.onStop();
}
// 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();
}