본문 바로가기

Programming/Android

[Android] 앱 캐시 제거(Webview 등)

오랜만에 글쓰네요..


프로젝트가 안정화 단계에 접어들다보니 포스팅할만한 내용이 없었네요.


하이브리드 앱을 제작하다보면 앱캐시로 인해 변경된 화면이 표시가 안되는 경우가 종종 있었는데요.


앱캐시를 확실하게 날릴 수 있는 코드입니다.


/**
 * 앱 캐시를 가차없이 지운다.
 */
public static void clearApplicationCache(Context context, File file) {

	File dir = null;

	if (file == null) {
		dir = context.getCacheDir();
	} else {
		dir = file;
	}

	if (dir == null)
		return;

	File[] children = dir.listFiles();
	try {
		for (int i = 0; i < children.length; i++)
			if (children[i].isDirectory())
				Util.clearApplicationCache(context, children[i]);
			else
				children[i].delete();
	} catch (Exception e) {
	}
};

어느 블로거님께서 쓰신글을 참조했는데요, recursive 하게 만드셨었는데 쓰신글 보면 함수가 완성이 덜되어 있더라구요.


그래서 수정하였습니다.


위와 같이 만들게 되면 앱캐시와 관련된 모든파일을 삭제하게 됩니다.


그러면 하이브리드에서의 캐시문제는 끝!!



참고 : http://ccdev.tistory.com/5