티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Android Webview - Completely Clear the Cache
Android Webview - 캐시 완전히 지우기
문제 내용
I have a WebView in one of my Activities, and when it loads a webpage, the page gathers some background data from Facebook.
제 액티비티 중 하나에 WebView가 있고, 웹페이지를 로드할 때 페이지는 Facebook에서 일부 백그라운드 데이터를 수집합니다.
What I'm seeing though, is the page displayed in the application is the same on each time the app is opened and refreshed.
그러나 보여지는 애플리케이션 페이지는 앱이 열리고 새로고침 될 때마다 동일합니다.
I've tried setting the WebView not to use cache and clear the cache and history of the WebView.
WebView가 캐시를 사용하지 않도록 설정하고 WebView의 캐시와 히스토리를 지우도록 시도해 보았지만 작동하지 않았습니다.
I've also followed the suggestion here: How to empty cache for WebView?
또한 여기에서 제안한 대로 시도해 봤지만: How to empty cache for WebView?
But none of this works, does anyone have any ideas of I can overcome this problem because it is a vital part of my application.
여기에 제안된대로 시도했지만 여전히 작동하지 않아서 문제를 해결할 방법이 있는지 알고 싶습니다. 이것은 제 애플리케이션의 중요한 부분입니다.
mWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
if(progress >= 100)
{
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
else
{
mProgressBar.setVisibility(ProgressBar.VISIBLE);
}
}
});
mWebView.setWebViewClient(new SignInFBWebViewClient(mUIHandler));
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.clearHistory();
mWebView.clearFormData();
mWebView.clearCache(true);
WebSettings webSettings = mWebView.getSettings();
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
Time time = new Time();
time.setToNow();
mWebView.loadUrl(mSocialProxy.getSignInURL()+"?time="+time.format("%Y%m%d%H%M%S"));
So I implemented the first suggestion (Although changed the code to be recursive)
그래서 나는 첫 번째 제안을 구현했습니다. (하지만 코드를 재귀적으로 변경했습니다.)
private void clearApplicationCache() {
File dir = getCacheDir();
if (dir != null && dir.isDirectory()) {
try {
ArrayList<File> stack = new ArrayList<File>();
// Initialise the list
File[] children = dir.listFiles();
for (File child : children) {
stack.add(child);
}
while (stack.size() > 0) {
Log.v(TAG, LOG_START + "Clearing the stack - " + stack.size());
File f = stack.get(stack.size() - 1);
if (f.isDirectory() == true) {
boolean empty = f.delete();
if (empty == false) {
File[] files = f.listFiles();
if (files.length != 0) {
for (File tmp : files) {
stack.add(tmp);
}
}
} else {
stack.remove(stack.size() - 1);
}
} else {
f.delete();
stack.remove(stack.size() - 1);
}
}
} catch (Exception e) {
Log.e(TAG, LOG_START + "Failed to clean the cache");
}
}
}
However this still hasn't changed what the page is displaying. On my desktop browser I am getting different html code to the web page produced in the WebView so I know the WebView must be caching somewhere.
그러나 이것은 여전히 페이지가 표시하는 것을 바꾸지 않았습니다. 데스크톱 브라우저에서는 WebView에서 생성된 웹 페이지와 다른 HTML 코드를 받고 있으므로 WebView가 어딘가에 캐싱되어 있다는 것을 알고 있습니다.
On the IRC channel I was pointed to a fix to remove caching from a URL Connection but can't see how to apply it to a WebView yet.
IRC 채널에서 URL 연결에서 캐싱을 제거하는 해결책을 제안했지만 아직 WebView에 적용하는 방법을 찾을 수 없습니다.
http://www.androidsnippets.org/snippets/45/
If I delete my application and re-install it, I can get the webpage back up to date, i.e. a non-cached version. The main problem is the changes are made to links in the webpage, so the front end of the webpage is completely unchanged.
앱을 삭제하고 다시 설치하면 웹 페이지를 최신 상태로 가져올 수 있습니다. 즉, 캐시되지 않은 버전입니다. 주요 문제는 웹 페이지에서 링크를 수정했기 때문에 웹 페이지의 프론트 엔드가 완전히 변경되지 않는 것입니다.
높은 점수를 받은 Solution
I found an even elegant and simple solution to clearing cache
더욱 우아하고 간단한 캐시 지우기 솔루션을 찾았습니다.
WebView obj;
obj.clearCache(true);
http://developer.android.com/reference/android/webkit/WebView.html#clearCache%28boolean%29
I have been trying to figure out the way to clear the cache, but all we could do from the above mentioned methods was remove the local files, but it never clean the RAM.
캐시를 지우는 방법을 알아보려고 노력해왔지만, 위에서 언급한 방법으로 할 수 있는 것은 로컬 파일을 제거하는 것뿐이며 RAM을 깨끗하게 지우지는 않습니다.
The API clearCache, frees up the RAM used by the webview and hence mandates that the webpage be loaded again.
API clearCache는 WebView에서 사용하는 RAM을 해제하므로 웹 페이지를 다시로드해야합니다.
가장 최근 달린 Solution
webView.clearCache(true)
appFormWebView.clearFormData()
appFormWebView.clearHistory()
appFormWebView.clearSslPreferences()
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
WebStorage.getInstance().deleteAllData()
출처 : https://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache
'개발 > 안드로이드' 카테고리의 다른 글
사용자가 선택하기 전에 Spinner에서 onItemSelected가 실행되지 않게 하는 방법 (0) | 2022.12.19 |
---|---|
'java.lang.IllegalStateException: Not allowed to start service Intent' 오류 수정하기 (0) | 2022.12.19 |
Android에서 문자열을 Uri로 바꾸기 (0) | 2022.12.18 |
버튼 클릭 시 새로운 액티비티 시작하기 (0) | 2022.12.18 |
컨텍스트로 레이아웃 인플레이터 가져오기 (0) | 2022.12.18 |