在 Android 的设置中,在“管理应用程序”活动中,单击应用程序时,数据会分解为应用程序、数据和缓存。还有一个清除缓存的按钮。我的应用程序缓存音频文件,我希望用户能够使用此按钮清除缓存。我如何存储它们,以便它们与缓存集中在一起并且用户可以清除它们?我尝试过使用以下两种技术来存储文件:
newFile = File.createTempFile("mcb", ".mp3", context.getCacheDir());
newFile = new File(context.getCacheDir(), "mcb.mp3");
newFile.createNewFile();
在这两种情况下,这些文件都列为数据而不是缓存。
我无法重现你的问题,也许还有其他问题。
使用以下应用程序,我能够在缓存目录中创建一个文件,然后使用“设置”下的“管理应用程序”功能清除它。我不需要改变清单中的任何内容,据我所知,你真的不应该搞乱android:allowClearUserData option http://groups.google.com/group/android-developers/browse_thread/thread/86b5bc388feed93e/0d70e7d7128b920b.
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();
}
}
});
}
}
运行应用程序后(并单击按钮创建文件):
$ adb -d shell
# ls /data/data/com.example.CreateCacheFile/cache
temp.txt
# cat /data/data/com.example.CreateCacheFile/cache/temp.txt
Hello World
#
管理应用程序报告有 4KB 空间用于缓存。单击按钮清除后:
# ls /data/data/com.example.CreateCacheFile/cache
# cat /data/data/com.example.CreateCacheFile/cache/temp.txt
temp.txt: No such file or directory
#
此时,管理应用程序报告有 0KB 空间用于缓存。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)