Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take advantage of Android's "Clear Cache" button

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.

like image 599
Jay Askren Avatar asked May 17 '10 12:05

Jay Askren


People also ask

Is it good to clear cache on Android?

Clearing your Android app cache can help fix speed issues and free up storage space. If you need more storage, clear the cache of the apps that take up the most space. Clearing your app cache every few months will help streamline your phone and keep it from getting too full.

What happens when you clear cache on Android?

When you use a browser, like Chrome, it saves some information from websites in its cache and cookies. Clearing them fixes certain problems, like loading or formatting issues on sites.

Is there a quick way to clear cache on Android?

Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.

Does clearing cache delete anything important?

Tip: Clearing the cache simply clears temporary files. It won't erase login credentials, downloaded files, or custom settings.


2 Answers

I can't reproduce your problem, perhaps something else is wrong.

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
# 

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
# 

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

like image 91
Tim Kryger Avatar answered Oct 12 '22 11:10

Tim Kryger


I think you have to make use of android:allowClearUserData and android:manageSpaceActivity under your <application> tag to see that option in 'Manage Applications'. See http://developer.android.com/guide/topics/manifest/application-element.html for more information.

Alternatively, have an option for clearing cache in your activity.

like image 37
Al. Avatar answered Oct 12 '22 12:10

Al.