Skip to content Skip to sidebar Skip to footer

Android Clear Cache

I want to click on the preference is cleared the cache of my app. I did this, but it does not work and there are errors. How can I fix? This is the whole source. Many Thanks!!!!!!

Solution 1:

here's my suggestion of how to fix your errors.

i could make it even better, but it should work just fine.

cache.setOnPreferenceClickListener(new OnPreferenceClickListener() 
  {
  clearCache(getApplicationContext());
  }

and here are the functions , fixed:

publicstaticvoid clearCache(final Context context)
  {
  final File cache=context.getCacheDir();
  final File appDir=new File(cache.getParent());
  if(appDir.exists())
    {
    // you might be able to change this whole code block to just "deleteDir(appDir)"finalString[] children=appDir.list();
    for(finalString childFilePath : children)
      if(!childFilePath.equals("lib"))
        {
        deleteDir(new File(appDir,childFilePath));
        Log.i("TAG","**************** File /data/data/APP_PACKAGE/"+childFilePath+" DELETED *******************");
        }
    }
  }

publicstaticboolean deleteDir(final File dir)
  {
  if(dir==null)
    returntrue;
  if(dir.isDirectory())
    {
    finalString[] children=dir.list();
    for(finalString childFilePath : children)
      {
      finalboolean success=deleteDir(new File(dir,childFilePath));
      if(!success)
        returnfalse;
      }
    }
  return dir.delete();
  }

here's the entire class code :

publicclassImpoextendsPreferenceActivity {

    Preference info;
    Intent intent;
    Preference cache;

    @OverrideprotectedvoidonCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.xml.layout);

        info = this.findPreference("info");
        info.setOnPreferenceClickListener(newOnPreferenceClickListener() {
            @OverridepublicbooleanonPreferenceClick(final Preference preference) {
                // TODO the next line is very weird. sure that's what you want?
                intent = newIntent(getBaseContext(), Info.class);
                startActivity(intent);
                returntrue;
            }
        });
        cache = this.findPreference("cache");
        cache.setOnPreferenceClickListener(newOnPreferenceClickListener() {

            @OverridepublicbooleanonPreferenceClick(final Preference preference) {
                clearCache(Impo.this);
                returntrue;
            }
        });
    }

    publicstaticvoidclearCache(final Context context) {
        finalFilecache= context.getCacheDir();
        finalFileappDir=newFile(cache.getParent());
        if (appDir.exists()) {
            // you might be able to change this whole code block to just "deleteDir(appDir)"final String[] children = appDir.list();
            for (final String childFilePath : children)
                if (!childFilePath.equals("lib")) {
                    deleteDir(newFile(appDir, childFilePath));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + childFilePath
                            + " DELETED *******************");
                }
        }
    }

    publicstaticbooleandeleteDir(final File dir) {
        if (dir == null)
            returntrue;
        if (dir.isDirectory()) {
            final String[] children = dir.list();
            for (final String childFilePath : children) {
                finalbooleansuccess= deleteDir(newFile(dir, childFilePath));
                if (!success)
                    returnfalse;
            }
        }
        return dir.delete();
    }
}

Post a Comment for "Android Clear Cache"