Clearing cache on exit

Put your problem here if it does not fit any of the other categories.

Clearing cache on exit

Postby alexandroidftw » Tue Oct 27, 2009 1:13 am

i would like to clear the cache of my app upon it closing this is source code i found elsewere and i need help with that activity to put it under

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. 1.public static void trimCache(Context context) {
  2.  
  3. 2.    try {
  4.  
  5. 3.        File dir = context.getCacheDir();
  6.  
  7. 4.        if (dir != null && dir.isDirectory()) {
  8.  
  9. 5.            deleteDir(dir);
  10.  
  11. 6.
  12.  
  13. 7.
  14.  
  15. 8.        }
  16.  
  17. 9.    } catch (Exception e) {
  18.  
  19. 10.        // TODO: handle exception
  20.  
  21. 11.    }
  22.  
  23. 12.
  24.  
  25. 13.
  26.  
  27. 14.}
  28.  
  29. 15.
  30.  
  31. 16.
  32.  
  33. 17.public static boolean deleteDir(File dir) {
  34.  
  35. 18.    if (dir!=null && dir.isDirectory()) {
  36.  
  37. 19.        String[] children = dir.list();
  38.  
  39. 20.        for (int i = 0; i < children.length; i++) {
  40.  
  41. 21.            boolean success = deleteDir(new File(dir, children[i]));
  42.  
  43. 22.            if (!success) {
  44.  
  45. 23.                return false;
  46.  
  47. 24.            }
  48.  
  49. 25.        }
  50.  
  51. 26.    }
  52.  
  53. 27.
  54.  
  55. 28.
  56.  
  57. 29.    // The directory is now empty so delete it
  58.  
  59. 30.    return dir.delete();
  60.  
  61. 31.}
  62.  
  63. 32.}
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
alexandroidftw
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 22, 2009 1:26 am

Top

Postby Oleg » Mon Nov 02, 2009 11:21 am

Hi. I think you need to:
1) Create an activity
2) override void onDestroy() method in activity
3) put your code in this method
public class Hello extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
...
@Override
protected void onDestroy() {
super.onDestroy();
trimCache(this);
}
}
Oleg
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Wed Nov 28, 2007 1:49 pm
Location: Ukraine

Postby raghavendra » Wed Nov 04, 2009 9:46 am

Hi,

Thank You very much......... It works great....
raghavendra
Freshman
Freshman
 
Posts: 5
Joined: Fri Sep 25, 2009 2:09 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests