如何以编程方式管理应用缓存? (退出应用程序时清除它)
How do I manage app cache programmatically? (Clear it upon exiting the app)
我刚刚创建了一个 WebView 应用程序,它基本上只是打开一个新闻网站,并试图通过在缓存中存储一些内容来提高速度。出现的问题是,无论何时将一些新文章添加到实际页面,应用程序都不会显示它们,因为它已经将主页以及访问过的其他一些页面存储在缓存中。因此,例如,如果您首先打开该应用程序,上网几分钟,然后在几天后打开它,那么它不会有任何新文章,只有您第一次打开之前出现的文章应用
当用户 starts/kills 应用程序使用某些代码时,有什么方法可以有效地清除缓存?
对于此问题是否有任何其他不会使应用程序变慢的解决方案?
您可以使用clearCache 方法。您可以在每次加载应用程序时调用它,以便应用程序清除存储在上一个会话中的缓存。查看文档:https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean)
WebView mWebView;
mWebView.clearCache(true);
您传递 'true' 作为参数不仅清除 RAM 缓存:
Clears the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used.
创建服务并将其附加到应用程序。此服务应遵守应用程序寿命。
public class AppController extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
startService(new Intent(getApplicationContext(), AppLifeService.class));
} catch (Throwable e) {
e.printStackTrace();
}
}
}
AppLifeService.java
public class AppLifeService extends Service {
private static final String TAG = AppLifeService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e(TAG, "END");
try {
File cacheDir = this.getCacheDir();
if (null != cacheDir && cacheDir.exists()) {
forceDelete(cacheDir);
}
} catch (Exception e) {
e.printStackTrace();
}
// Stop the service itself
stopSelf();
}
@SuppressWarnings("UnusedReturnValue")
private boolean forceDelete(File file) {
File[] contents = file.listFiles();
if (null != contents) {
for (File f : contents) {
forceDelete(f);
}
}
return file.delete();
}
}
onTaskRemoved
在您的应用程序终止时被调用。所以这就是您在应用程序被杀死之前清除应用程序缓存的地方。
记得将此 类 添加到 Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.package">
<application
android:name=".AppController"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<service
android:name=".AppLifeService"
android:stopWithTask="false" />
</application>
</manifest>
我刚刚创建了一个 WebView 应用程序,它基本上只是打开一个新闻网站,并试图通过在缓存中存储一些内容来提高速度。出现的问题是,无论何时将一些新文章添加到实际页面,应用程序都不会显示它们,因为它已经将主页以及访问过的其他一些页面存储在缓存中。因此,例如,如果您首先打开该应用程序,上网几分钟,然后在几天后打开它,那么它不会有任何新文章,只有您第一次打开之前出现的文章应用
当用户 starts/kills 应用程序使用某些代码时,有什么方法可以有效地清除缓存?
对于此问题是否有任何其他不会使应用程序变慢的解决方案?
您可以使用clearCache 方法。您可以在每次加载应用程序时调用它,以便应用程序清除存储在上一个会话中的缓存。查看文档:https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean)
WebView mWebView;
mWebView.clearCache(true);
您传递 'true' 作为参数不仅清除 RAM 缓存:
Clears the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used.
创建服务并将其附加到应用程序。此服务应遵守应用程序寿命。
public class AppController extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
startService(new Intent(getApplicationContext(), AppLifeService.class));
} catch (Throwable e) {
e.printStackTrace();
}
}
}
AppLifeService.java
public class AppLifeService extends Service {
private static final String TAG = AppLifeService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e(TAG, "END");
try {
File cacheDir = this.getCacheDir();
if (null != cacheDir && cacheDir.exists()) {
forceDelete(cacheDir);
}
} catch (Exception e) {
e.printStackTrace();
}
// Stop the service itself
stopSelf();
}
@SuppressWarnings("UnusedReturnValue")
private boolean forceDelete(File file) {
File[] contents = file.listFiles();
if (null != contents) {
for (File f : contents) {
forceDelete(f);
}
}
return file.delete();
}
}
onTaskRemoved
在您的应用程序终止时被调用。所以这就是您在应用程序被杀死之前清除应用程序缓存的地方。
记得将此 类 添加到 Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.package">
<application
android:name=".AppController"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<service
android:name=".AppLifeService"
android:stopWithTask="false" />
</application>
</manifest>