1. /*
2. * 文 件 名: DataCleanManager.java
3. * 描 述: 主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录
4. */
5. package com.test.DataClean;
6.
7. import java.io.File;
8.
9. import android.content.Context;
10. import android.os.Environment;
11.
12. /**
13. * 本应用数据清除管理器
14. */
15. public class DataCleanManager {
16. /**
17. * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache)
18. *
19. * @param context
20. */
21. public static void cleanInternalCache(Context context) {
22. deleteFilesByDirectory(context.getCacheDir());
23. }
24.
25. /**
26. * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases)
27. *
28. * @param context
29. */
30. public static void cleanDatabases(Context context) {
31. new File("/data/data/"
32. "/databases"));
33. }
34.
35. /**
36. * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs)
37. *
38. * @param context
39. */
40. public static void cleanSharedPreference(Context context) {
41. new File("/data/data/"
42. "/shared_prefs"));
43. }
44.
45. /**
46. * 按名字清除本应用数据库
47. *
48. * @param context
49. * @param dbName
50. */
51. public static void cleanDatabaseByName(Context context, String dbName) {
52. context.deleteDatabase(dbName);
53. }
54.
55. /**
56. * 清除/data/data/com.xxx.xxx/files下的内容
57. *
58. * @param context
59. */
60. public static void cleanFiles(Context context) {
61. deleteFilesByDirectory(context.getFilesDir());
62. }
63.
64. /**
65. * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache)
66. *
67. * @param context
68. */
69. public static void cleanExternalCache(Context context) {
70. if (Environment.getExternalStorageState().equals(
71. Environment.MEDIA_MOUNTED)) {
72. deleteFilesByDirectory(context.getExternalCacheDir());
73. }
74. }
75.
76. /**
77. * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除
78. *
79. * @param filePath
80. */
81. public static void cleanCustomCache(String filePath) {
82. new File(filePath));
83. }
84.
85. /**
86. * 清除本应用所有的数据
87. *
88. * @param context
89. * @param filepath
90. */
91. public static void cleanApplicationData(Context context, String... filepath) {
92. cleanInternalCache(context);
93. cleanExternalCache(context);
94. cleanDatabases(context);
95. cleanSharedPreference(context);
96. cleanFiles(context);
97. for (String filePath : filepath) {
98. cleanCustomCache(filePath);
99. }
100. }
101.
102. /**
103. * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理
104. *
105. * @param directory
106. */
107. private static void deleteFilesByDirectory(File directory) {
108. if (directory != null && directory.exists() && directory.isDirectory()) {
109. for (File item : directory.listFiles()) {
110. item.delete();
111. }
112. }
113. }
114. }
清除应用程序所有缓存
原创
©著作权归作者所有:来自51CTO博客作者lishuai052086的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
mui 应用程序启动是自动执行清除缓存任务
做h5+App时,远程服务器放了一个封面文件夹,方便根据情况更换程序封面。但是由于没有更新缓存,所以服务器图片
缓存 javascript html 加载 h5 -
HTML 5 学习之应用程序缓存
什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问。 应用程序缓存为应用带来三个优势:
web.xml java javascript ViewUI 缓存 -
应用程序缓存
本篇文章介绍html5应用程序缓存。
html5 浏览器 缓存 html 服务器 -
android 显示所有应用程序入口
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),今天这讲是OptionsMenu 一、概述 public boolean onCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu 。 public bool
android 显示所有应用程序入口 android 菜单学习 android菜单应该 菜单 android