webview.clearCache from PreferenceActivity

后端 未结 2 614
北荒
北荒 2021-01-16 11:51

I have preferences.xml which is used in my extension of PreferencesActivity.

I have another preference in the xml that I would like to use to clear the cache of a w

相关标签:
2条回答
  • 2021-01-16 12:14

    Solved by adding:

        Preference myPref = findPreference("myPref");
        myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    
            @Override
            public boolean onPreferenceClick(Preference preference) {
                MyActivity.mWebView.clearCache(true);
                return false;
            }
        });
    
    0 讨论(0)
  • 2021-01-16 12:28

    Thanks for your own answer :) It was usefull for me.

    In addition: below is the code when you want to delete a directory/folder on the sd-card from your xml-style preferences:

        public class Preferences extends PreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
    
            Preference myPref = findPreference("myPref");
            myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    File sdcard = Environment.getExternalStorageDirectory();
                    String sdcardPath = sdcard.getAbsolutePath();
                    File mDbFile = new File(sdcardPath + "/myPath/");
                    if(mDbFile.exists()) {
                        deleteDirectory(mDbFile);
                    }
                    return false;
                }
            });
        }
    
        private static boolean deleteDirectory(File path) {
            if( path.exists() ) {
              File[] files = path.listFiles();
              for(int i=0; i<files.length; i++) {
                 if(files[i].isDirectory()) {
                   deleteDirectory(files[i]);
                 }
                 else {
                   files[i].delete();
                 }
              }
            }
            return( path.delete() );
          }
    }
    
    0 讨论(0)
提交回复
热议问题