android - 以编程方式清除android应用程序中的缓存

标签 android performance caching

我需要有关我的 Android 应用程序中缓存内存的帮助。我在设备中运行服务器(android)。我想以编程方式清除该应用程序的缓存。我在该服务器中有数据库。基于该数据库,我的客户操作正在进行中。所以我不希望它(数据库)受到影响。我只想清除缓存而不是清除数据。请帮我解决这个问题。

最佳答案

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) {
         e.printStackTrace();
      }
   }

   public static void trimCache(Context context) {
      try {
         File dir = context.getCacheDir();
         deleteDir(dir);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   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;
      }
   }

关于android - 以编程方式清除android应用程序中的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28492956/

相关文章:

android - 在 RecycleView 中有效加载数据

javascript - 在 Meteor 应用程序中使用 appcache

android - 清除应用程序数据库

android - WebView android 应用程序中的推送通知

android - 升级 Android Studio 2.3 后数据绑定(bind)不起作用

java - Android Studio 无法在 Mac OS Snow Leopard 上打开 - java.lang.NoClassDefFoundError : java. awt.Toolkit

docker - 如何在 Docker 中挂载 NextJS 缓存

android - 通过 Bundle 传递 ArrayList 对象?

php - 使用ajax每秒访问数据库

PHP:重用对象或复制到数组中更快吗