如何在Android的全球声明资源 [英] How to declare a resource globally in Android

查看:185
本文介绍了如何在Android的全球声明资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示相册列表,然后,一旦选择一个专辑,在这张专辑显示照片的应用程序。我使用从谷歌的例子(因为照片是从网站下载)一个内存缓存/磁盘缓存实现。一切工作正常,但磁盘高速缓存初始化发生每年选择一个专辑的时候,和初始化需要相当长的时间。我想声明的磁盘缓存全球化,并使用它的所有专辑。我不是在Java中的专家,并不清楚如何做到这一点,特别是各种活动被称为从一个活动切换到另一个时,我不能只传递一个参考缓存。如果整个缓存逻辑来打造为呼吁在需要的基础是服务呢?还是有这样做的不同的和/或更好/更优雅的方式?

I have an application that displays a list of photo albums and then, once an album is selected, displays photos in that album. I am using the memory cache/disk cache implementation from one of the google examples (since the photos are loaded from a website). Everything is working fine, but the disk cache initialization takes place every time an album is chosen, and the initialization takes considerable amount of time. I'd like to declare the disk cache "globally" and use it for all albums. I am not an expert in Java and not clear how to do this, particularly that various activities are being called and I can't just pass a reference to the cache when switching from one activity to another. Should the entire caching logic be build as a "service" and then "called" upon on as needed basis? Or is there a different and/or better/more elegant way of doing this?

感谢你,

推荐答案

延长应用程序类。

在这里你就是一个例子:

Here you are an example:

public class MyApplication extends Application {

    private static MyApplication singleInstance;
    //TODO: Your fields here

    public void onCreate() {
        super.onCreate();
        MyApplication.singleInstance = (MyApplication)getApplicationContext();
        //TODO: Your initialization code here
    }

    public static MyApplication getStaticApplicationContext() {
        return singleInstance;
    }

    //TODO: Your methods here
}

有您可以添加缓存和相关的code。

There you can add your cache and the relevant code.

您必须引用类到的Andr​​oidManifest.xml 文件,添加的android:名称属性,在应用标签,就像这样:

You will have to reference the class into the AndroidManifest.xml file, adding the android:name attribute in the application tag, like this:

<manifest ...>
    ...
    <application ...
        android:name="com.example.app.MyApplication">
    ...
</manifest>

这篇关于如何在Android的全球声明资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆