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

后端 未结 2 614
萌比男神i
萌比男神i 2021-01-31 00:18

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 cl

2条回答
  •  清酒与你
    2021-01-31 00:42

    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.

提交回复
热议问题