退出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 -

  • how-to-clear-data-cache-of-the-application-through-$c$c