【问题标题】:Android Clear Cache安卓清除缓存
【发布时间】:2013-07-11 02:48:54
【问题描述】:

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

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

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

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

【问题讨论】:

  • “它不起作用并且有错误”。所以,告诉我们你遇到了什么错误,告诉我们你对 Log 的调用显示了什么,告诉我们什么不起作用。
  • 好的。 eclipse 在此处标记错误 File appDir = new File(cache.getParent());,此处为 },此处为 public static boolean deleteDir(File dir),此处为 if (dir != null && dir.isDirectory()) {
  • 你导入 File 类了吗?
  • 是的。我要疯了。请帮帮我:'(

标签: java android xml caching


【解决方案1】:

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

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

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

【讨论】:

  • 我写过函数。函数不能写在其他函数中。请把“}”放在正确的地方,然后再检查一遍。此外,您所显示的内容不显示错误,只显示它们的标记。错误写在 Eclipse 的“问题”视图中(使用 ALT+SHIFT+Q,X 来显示)。我再次编辑了我的答案,以便您看到它们是分开的。
  • 我把作品放出来了,但是这里有一个错误 clearCache(getApplicationContext());此行有多个标记 - 语法错误,插入“}”以完成 ClassBody - 语法错误,插入“;”完成 ConstructorDeclaration
  • 你的代码不会编译,不是因为我写的函数,而是因为你写在那里的位置。看看“cache.setOnPreferenceClickListener”之后的行 - 而不是实现你把我写的功能放在那里的界面。同样,它们应该在任何方法或函数之外。它们不应该是你放置它们的地方。
  • 我尝试了很多方法,但是eclipse总是报错。你能帮我把正确的代码发布给 Magati 吗?
  • 什么是magati?请发布整个代码,我会修复它,但你必须学习如何阅读你的代码。这是一个非常基本的事情。也许你应该看“新波士顿”系列学习java:thenewboston.org/list.php?cat=31
猜你喜欢
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 2017-04-06
  • 1970-01-01
  • 2016-04-09
相关资源
最近更新 更多