简体   繁体   English

如何以编程方式管理应用缓存? (退出应用程序后清除)

[英]How do I manage app cache programmatically? (Clear it upon exiting the app)

I've just created a WebView app that just basically opens a news website and tried to make it faster by storing some stuff in cache. 我刚刚创建了一个WebView应用程序,该应用程序基本上只是打开一个新闻网站,并试图通过在缓存中存储一​​些内容来使其更快。 The problem that came up was that whenever some new articles were added to the actual page, the app would not show them, since it already has the main page, along with some others that were visited, stored in cache. 出现的问题是,每当将一些新文章添加到实际页面时,该应用程序都不会显示它们,因为它已经具有主页以及其他一些已访问的页面存储在缓存中。 So if, for example, you first opened the app, surfed the web for a couple of minutes, and then opened it after a couple of days, it wouldn't have any new articles, just the ones that came before you first opened the app. 因此,例如,如果您首先打开该应用程序,在网上浏览了几分钟,然后在几天后打开它,那么它将没有任何新的文章,只有您第一次打开该文章之前的文章。应用程式。

Is there any way I can effectively clear the cache when a user starts/kills the app using some code? 用户使用某些代码启动/杀死应用程序时,有什么方法可以有效清除缓存?

Are there any other solutions to this problem that would not make the app much slower? 是否有其他解决方案可以使应用程序变慢?

You can use the clearCache method. 您可以使用clearCache方法。 You may call it every time the application load, to may the app clear the cache stored on the previous session. 您可以在每次应用程序加载时调用它,以使应用程序清除上一个会话中存储的缓存。 Check out the documentation: https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean) 查看文档: https : //developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean)

WebView mWebView;
mWebView.clearCache(true);

you pass 'true' as parameters to clear not only the RAM cache: 您传递“ true”作为参数不仅要清除RAM缓存:

Clears the resource cache. 清除资源缓存。 Note that the cache is per-application, so this will clear the cache for all WebViews used. 请注意,缓存是针对每个应用程序的,因此这将清除所有使用的WebView的缓存。

Create a service and attach it to application. 创建服务并将其附加到应用程序。 this service should observe application life. 该服务应遵守应用程序寿命。

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 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 gets called when your application is terminated. 当您的应用程序终止时,将调用onTaskRemoved so that's where you clear application cache right before app is killed. 这样一来,您就可以在杀死应用之前清除应用缓存。

remember to add this classes to 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM