我该如何利用Android的&QUOT的;清除缓存"钮 [英] How do I take advantage of Android's "Clear Cache" button

查看:217
本文介绍了我该如何利用Android的&QUOT的;清除缓存"钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的设置,在一个应用程序时,点击管理应用程序的活动,该数据被分解成应用程序,数据和缓存。还有一个按钮来清除缓存。我的应用程序缓存的音频文件,我想用户能够清除使用此按钮缓存。如何存储他们,使他们得到集中在缓存中,并且用户可以清除它们?我试着存储使用以下两种技术文件:

In Android's settings, in the "Manage Applications" activity when clicking on an app, the data is broken down into Application, Data, and cache. There is also a button to clear the cache. My app caches audio files and I would like the user to be able to clear the cache using this button. How do I store them so they get lumped in with the cache and the user can clear them? I've tried storing files using both of the following techniques:

newFile = File.createTempFile("mcb", ".mp3", context.getCacheDir());


newFile = new File(context.getCacheDir(), "mcb.mp3");
newFile.createNewFile();

在这两种情况下,这些文件被列为数据,而不是高速缓存

In both cases, these files are listed as Data and not Cache.

推荐答案

我无法重现您的问题,也许是别的东西是错误的。

使用下面的应用程序,我能够在缓存中创建目录中的文件,然后将其清除使用设置下的管理应用程序的功能。我没有在清单中,并从改变什么我可以告诉,<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/86b5bc388feed93e/0d70e7d7128b920b">you真的不应该乱用机器人:allowClearUserData 选项

Using the following application, I was able to create a file in the cache directory and then clear it using the Manage Applications function under Settings. I didn't have to change anything in the manifest and from what I can tell, you really shouldn't mess with the android:allowClearUserData option.

public class CreateCacheFile extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.CreateFileButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(
                        CreateCacheFile.this.getCacheDir(), "temp.txt");

                try {
                    file.createNewFile();
                    FileWriter fw = new FileWriter(file);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write("Hello World");
                    bw.newLine();
                    bw.close();

                } catch (IOException e) {
                    Toast.makeText(
                            CreateCacheFile.this, 
                            "Error!", 
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

运行的应用程序(和点击按钮来创建文件)后:

After running the application (and clicking the button to create the file):

$ adb -d shell
# ls /data/data/com.example.CreateCacheFile/cache
temp.txt
# cat /data/data/com.example.CreateCacheFile/cache/temp.txt
Hello World
# 

管理应用程序报告认为,空间的4KB是在使用高速缓存。点击按钮后,将其清除:

Manage Applications reports that 4KB of space is in use for Cache. After clicking the button to clear it:

# ls /data/data/com.example.CreateCacheFile/cache
# cat /data/data/com.example.CreateCacheFile/cache/temp.txt
temp.txt: No such file or directory
# 

在这一点上管理应用程序报告说,空间0KB正在使用的缓存。

At this point Manage Applications reports that 0KB of space is in use for Cache.

这篇关于我该如何利用Android的&QUOT的;清除缓存&QUOT;钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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