如何避免安装和关闭HttpResponseCache'在每个活动和服务?

时间:2014-09-02 11:07:14

标签: java android caching networking

我正在开发一款兼容API 14(冰淇淋三明治)的Android应用。我找到了可以用来缓存HTTP请求的类HttpResponseCache。我想将缓存与我的HTTP请求代码(一个带有一堆public static方法的公共类)集成 HttpResponseCache文档说明必须在使用的每个活动或服务上安装和关闭缓存。我希望我的应用程序始终启用缓存,但我不想记住安装缓存并在我开发的每个新活动上关闭它。

是否有实现这一目标的模式?

2 个答案:

答案 0 :(得分:2)

创建应用程序的进程时,首先创建Application实例。使用Application的{​​{1}}方法安装缓存是最佳选择。在创建任何活动/服务/广播接收器实例之前调用onCreate方法。

当活动停止时,我们需要将缓冲的内容刷新到文件系统,以便下次活动开始时缓存可读。我们可以注册活动生命周期回调并在Application.onCreate回调中调用HttpResponseCache.flush方法。

onActivityStopped

以上是基本答案;以下是特殊情况。

服务

对于服务,没有这样的通用回调接口。因此,必须从每个服务的public class MyApplication extends Application { public void onCreate() { super.onCreate(); // Install the cache try { File httpCacheDir = new File(this.getCacheDir(), "http"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); catch (IOException e) { Log.i(TAG, "HTTP response cache installation failed:" + e); } // Register for activity lifecycle callbacks, // specifically interested in activity stop callbacks. registerActivityLifecycleCallbacks( new MyApplicationActivityLifeCycleCallbacks()); } // method to flush cache contents to the filesystem public void flushCache() { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } } private class MyApplicationActivityLifeCycleCallbacks extends ActivityLifecycleCallbacks { public void onActivityStopped (Activity activity) { flushCache(); } // other methods } } 方法调用flush

onDestroy

请记住在Android清单文件的public class MyService extends Service { public void onDestroy() { super.onDestroy(); MyApplication application = (MyApplication) getApplication(); application.flushCache(); } } 标记中指定应用程序名称。

替代解决方案:要刷新所有服务中的内容,您可以编写基本服务代码,以刷新其<application>中的内容。然后,应用程序中的所有服务都可以从基本服务扩展。

onDestroy

关闭缓存

如果您特别关注在最后一个组件出口处关闭缓存,则可以跟踪应用程序中的组件实例数。第一个实例创建缓存,最后一个实例关闭它。

MyApplication.java :跟踪应用程序中组件实例的数量。

public class BaseService extends Service {
    public void onDestroy() {
        super.onDestroy();

        HttpResponseCache cache = HttpResponseCache.getInstalled();
        if (cache != null) {
            cache.flush();
        }
    }
}

public class MyService extends BaseService {
    ....
}

BaseActivity.java :您的所有活动类都扩展了此BaseActivity。

public class MyApplication extends Application {
    private int instanceCount;

    public void incrementInstanceCount() {
        instanceCount++;
    }

    public int decrementInstanceCount() {
        return --instanceCount;
    }
}

BaseService.java :从BaseService扩展所有服务类

public class BaseActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if( // cache is not installed ) {
            // install the cache
        }
        getApplication().incrementInstanceCount();
    }

    public void onStop() {
        super.onStop();
        // flush the cache
    }

    public void onDestroy() {
        super.onDestroy();

        if( getApplication().decrementInstanceCount() == 0) {
            // close the cache
        }
    }
}

答案 1 :(得分:0)

你可以使用

 yourConnection.setUseCaches(boolean);
相关问题