-1

我想点击首选项清除我的应用程序的缓存。我这样做了,但它不起作用并且有错误。我该如何解决?

这就是全部来源。非常感谢!!!!!!

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();
}
}

……………………………………………………………………………………………………………………………………

4

1 回答 1

0

这是我关于如何修复错误的建议。

我可以让它变得更好,但它应该工作得很好。

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

这是固定的功能:

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();
  }

这是整个课程代码:

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();
    }
}
于 2013-07-13T11:41:47.690 回答