Clear Cache App: Why it doesn't work?

一曲冷凌霜 提交于 2019-12-12 06:39:08

问题


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!!!!!!

public class Impo extends PreferenceActivity{


Preference info;
Intent intent;
Preference cache;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.layout);


info= (Preference) this.findPreference("info");
info.setOnPreferenceClickListener( new OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference preference) {
        intent = new Intent(getBaseContext(), Info.class);
        startActivity(intent);
        return true;
    }});
cache = (Preference)this.findPreference("cache");
cache.setOnPreferenceClickListener(new OnPreferenceClickListener() {

}
}
public static void 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)"
  final String[] children=appDir.list();
  for(final String childFilePath : children)
    if(!childFilePath.equals("lib"))
      {
      deleteDir(new File(appDir,childFilePath));
      Log.i("TAG","**************** File /data/data/APP_PACKAGE/"+childFilePath+" DELETED *******************");
      }
  }
}

public static boolean deleteDir(final File dir)
{
if(dir==null)
  return true;
if(dir.isDirectory())
  {
  final String[] children=dir.list();
  for(final String childFilePath : children)
    {
    final boolean success=deleteDir(new File(dir,childFilePath));
    if(!success)
      return false;
    }
  }
return dir.delete();
}
}

.......................................


回答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:

public static void 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)"
    final String[] children=appDir.list();
    for(final String childFilePath : children)
      if(!childFilePath.equals("lib"))
        {
        deleteDir(new File(appDir,childFilePath));
        Log.i("TAG","**************** File /data/data/APP_PACKAGE/"+childFilePath+" DELETED *******************");
        }
    }
  }

public static boolean deleteDir(final File dir)
  {
  if(dir==null)
    return true;
  if(dir.isDirectory())
    {
    final String[] children=dir.list();
    for(final String childFilePath : children)
      {
      final boolean success=deleteDir(new File(dir,childFilePath));
      if(!success)
        return false;
      }
    }
  return dir.delete();
  }

here's the entire class code :

public class Impo extends PreferenceActivity {

    Preference info;
    Intent intent;
    Preference cache;

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

        info = this.findPreference("info");
        info.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(final Preference preference) {
                // TODO the next line is very weird. sure that's what you want?
                intent = new Intent(getBaseContext(), Info.class);
                startActivity(intent);
                return true;
            }
        });
        cache = this.findPreference("cache");
        cache.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(final Preference preference) {
                clearCache(Impo.this);
                return true;
            }
        });
    }

    public static void 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)"
            final String[] children = appDir.list();
            for (final String childFilePath : children)
                if (!childFilePath.equals("lib")) {
                    deleteDir(new File(appDir, childFilePath));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + childFilePath
                            + " DELETED *******************");
                }
        }
    }

    public static boolean deleteDir(final File dir) {
        if (dir == null)
            return true;
        if (dir.isDirectory()) {
            final String[] children = dir.list();
            for (final String childFilePath : children) {
                final boolean success = deleteDir(new File(dir, childFilePath));
                if (!success)
                    return false;
            }
        }
        return dir.delete();
    }
}


来源:https://stackoverflow.com/questions/17616966/clear-cache-app-why-it-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!