2

如何执行 Clear(); 在另一个活动的 FileCache.class 中。我正在展示我的编码的一小部分。我的目标是在每次退出时清除外部缓存文​​件。谁能告诉我它是如何完成的。谢谢

FileCache.class

public class FileCache {

    private File cacheDir;

    public FileCache(Context context){
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }

    public File getFile(String url){
        //I identify images by hashcode. Not a perfect solution, good for the demo.
        String filename=String.valueOf(url.hashCode());
        //Another possible solution (thanks to grantland)
        //String filename = URLEncoder.encode(url);
        File f = new File(cacheDir, filename);
        return f;

    }

    public void clear(){
        File[] files=cacheDir.listFiles();
        if(files==null)
            return;
        for(File f:files)
            f.delete();
    }

}

MyMainActivity.class

@Override
        public void onBackPressed() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Do you want to exit?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                           Intent intent = new Intent(Intent.ACTION_MAIN);
                           intent.addCategory(Intent.CATEGORY_HOME);
                           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                           startActivity(intent);
                           new clear();  //<<--- How do i Call clear(); in FileCache.class
                           System.exit(0);
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();
            alert.show();

    }
4

4 回答 4

2

试试这个。

public void onClick(DialogInterface dialog, int id) {

                               Intent intent = new Intent(Intent.ACTION_MAIN);
                               intent.addCategory(Intent.CATEGORY_HOME);
                               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                               startActivity(intent);
                               FileCache loader = new FileCache(null);
                               loader.clear();  
                               System.exit(0);
                           }
于 2012-08-31T10:46:56.313 回答
1

像这样的东西?

public void onClick(DialogInterface dialog, int id) {
    new  FileCache( MyMainActivity.this ).clear();
}
于 2012-08-31T10:53:25.907 回答
0

我相信您已经实现了Android 中图像延迟加载的解决方案。

ImageLoader 类中已经给出了 clearCache() 方法:

 public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

因此,您可以通过调用 clearCache() 方法清除缓存,如下所示:

ImageLoader imgLoader = new ImageLoader(mContext);
imgLoader.clearCache();
于 2012-08-31T10:51:11.827 回答
0

您需要对 FileCache 对象的引用。我想你在onCreate()from 活动中创建它。如果是这样,请使 FileCache 成为活动的属性。这样,您就可以myFileCacheReference.clear()onBackPressed().

public class MainActivity extends Activity {
  private FileCache myFileCacheRef;

  public void onCreate(Bundle b) {
    //standard stuff
    myFileCacheRef = new FileCache();
  }

  public void onBackPressed() {
    myFileCacheRef.clear();
  }
}

像这样的东西应该工作。

于 2012-08-31T10:53:46.797 回答