【问题标题】:Unable to Clear Cache memory programmatically Android无法以编程方式清除缓存内存 Android
【发布时间】:2019-11-24 12:46:25
【问题描述】:

我已尝试使用以下代码通过应用程序中的按钮清除缓存。
activity_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>

MainActivity.java

public class MainActivity extends AppCompatActivity  {
TextView t1;
EditText et1;
Button b1,b2,b3;
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings;
SharedPreferences.Editor editor;
private static MainActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    settings = getSharedPreferences(MainActivity.PREFS_NAME, 0);
    editor= settings.edit();
    instance=this;
    t1=(TextView)findViewById(R.id.textView);
    et1=(EditText)findViewById(R.id.editText);
    b1=(Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editor.putString("editText",et1.getText().toString());
            editor.commit();
            t1.setText(settings.getString("editText",null));
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t1.setText("");
            MainActivity.getInstance().clearApplicationData();
                        }
    });
    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t1.setText(settings.getString("editText",null));
        }
    });
}
public static MainActivity getInstance(){ return instance;}
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();
    }
    else {
        return false;
    }
}}

说明:
当点击 Save 按钮时,EditText 中输入的值将保存在 Shared Preference 中。点击 Delete Cache 时,编写删除缓存的代码与@987654321 @,Refer 2。当点击Retrive时,存储在Shared Preference中的值将被检索并显示在TextView中。

问题:在我的 logcat 中单击 Delete Cache 时,它显示 缓存内存已删除 并且 shared_prefs 已删除但我的缓存大小没有减少。在点击 Retrive 时,Shared Preference 中的值正在检索并显示在 TextView 中,但应该删除该值。

我的日志猫:

01-09 17:02:26.872 24290-24290/? I/TAG: File /data/data/APP_PACKAGE/ cache DELETED
01-09 17:02:26.872 24290-24290/? I/TAG: File /data/data/APP_PACKAGE/ shared_prefs DELETED

我错过了什么?

【问题讨论】:

    标签: java android


    【解决方案1】:

    onStop() 和 onDestroy() 方法包含在您的 Activity 中。您的 Activity 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();
     }
    

    【讨论】:

    • 我想在点击按钮时清除缓存。我替换了您的 trimCache() 和 dleteDir()。仍然没有改善
    • 您的活动中是否包含 onDestroy()?
    • 我认为 onDestroy() 是必需的!在 onClick() 中调用 trimCache()?但我不想关闭我的活动。
    • 您的活动在此过程中也在缓存内存中...如果您不关闭活动意味着它如何清除缓存?。明白我的意思吗?
    • 感谢您的解释。我加入了onDestroy(),但仍然没有改善。再次打开活动时,它会检索数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多