Clear webview cache in broadcast receiver?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I would like to clear the webview cache whenver a user updates my app. I have tried looking up at a lot of solutions but none seem to address this specifically.
I have registered the broadcast receiver as follows.
Manifest file :
<receiver android:name=".UpdateReceiver">
<intent-filter >
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.mypackage.com" />
</intent-filter>
</receiver>
Broadcast Receiver:
public class UpdateReceiver extends BroadcastReceiver
{
private WebView mWebview ;
@Override
public void onReceive(Context con, Intent intent)
{
mWebview = (WebView) findViewById(R.id.webView);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
mWebview.clearCache(true);
}
}
But, I cannot access the webview in the broadcast receiver. Any idea on how to get this done exactly? Maybe I'm getting something wrong.
Thanks.
java

add a comment |
I would like to clear the webview cache whenver a user updates my app. I have tried looking up at a lot of solutions but none seem to address this specifically.
I have registered the broadcast receiver as follows.
Manifest file :
<receiver android:name=".UpdateReceiver">
<intent-filter >
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.mypackage.com" />
</intent-filter>
</receiver>
Broadcast Receiver:
public class UpdateReceiver extends BroadcastReceiver
{
private WebView mWebview ;
@Override
public void onReceive(Context con, Intent intent)
{
mWebview = (WebView) findViewById(R.id.webView);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
mWebview.clearCache(true);
}
}
But, I cannot access the webview in the broadcast receiver. Any idea on how to get this done exactly? Maybe I'm getting something wrong.
Thanks.
java

add a comment |
I would like to clear the webview cache whenver a user updates my app. I have tried looking up at a lot of solutions but none seem to address this specifically.
I have registered the broadcast receiver as follows.
Manifest file :
<receiver android:name=".UpdateReceiver">
<intent-filter >
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.mypackage.com" />
</intent-filter>
</receiver>
Broadcast Receiver:
public class UpdateReceiver extends BroadcastReceiver
{
private WebView mWebview ;
@Override
public void onReceive(Context con, Intent intent)
{
mWebview = (WebView) findViewById(R.id.webView);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
mWebview.clearCache(true);
}
}
But, I cannot access the webview in the broadcast receiver. Any idea on how to get this done exactly? Maybe I'm getting something wrong.
Thanks.
java

I would like to clear the webview cache whenver a user updates my app. I have tried looking up at a lot of solutions but none seem to address this specifically.
I have registered the broadcast receiver as follows.
Manifest file :
<receiver android:name=".UpdateReceiver">
<intent-filter >
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.mypackage.com" />
</intent-filter>
</receiver>
Broadcast Receiver:
public class UpdateReceiver extends BroadcastReceiver
{
private WebView mWebview ;
@Override
public void onReceive(Context con, Intent intent)
{
mWebview = (WebView) findViewById(R.id.webView);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
mWebview.clearCache(true);
}
}
But, I cannot access the webview in the broadcast receiver. Any idea on how to get this done exactly? Maybe I'm getting something wrong.
Thanks.
java

java

asked Jan 3 at 14:09


Rahul SureshRahul Suresh
399
399
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The context
you are using to find your WebView
reference by findViewById
is the context
from the Receiver
and not a context
from an Activity
therefor it can't access the WebView
There are a lot of possibilities:
Solution 1
You can use LocalBroadcastReceiver
to communicate with the activity that holds the reference to your WebView
, from there you can properly clear the cache like you were trying to do in the receiver. mWebview.clearCache(true);
Solution 2
In the onReceive
of your BroadcastReceiver you can store a preference in the SharedPreferences
telling that an updated has occurred.
@Override
public void onReceive(Context con, Intent intent)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("hasAppUpdated", true).apply();
}
And then in your Activity
that deals with the WebView
you can verify if there was an App update and clear the cache of the WebView
.
To access the value from the SharedPreferences
:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean hasAppUpdated = prefs.getBoolean("hasAppUpdated", false);
if(hasAppUpdated) {
prefs.edit().putBoolean("hasAppUpdated", false).apply();
mWebview.clearCache(true);
}
The false
value is the default value for the hasAppUpdated
, and after getting the value from the preferences you might want to make the preference back to false again.
Ugly solution
Another way of doing that might be this ugly hack that I don't recommend.
I didn't test this but if you are using WebView
then the cache folder of your app probably contain a folder org.chromium.android_webview
(for versions bellow API 19 this might not be true since the WebView
back then didn't use chromium) and inside that folder you have the cache of your WebView
so if you remove the files inside this folder using the java.io.File
API you probably have your problem solved without requiring accessing the WebView
. Remember that this is a super ugly solution(if it even works) and is far from being a bullet-proof solution.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023956%2fclear-webview-cache-in-broadcast-receiver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The context
you are using to find your WebView
reference by findViewById
is the context
from the Receiver
and not a context
from an Activity
therefor it can't access the WebView
There are a lot of possibilities:
Solution 1
You can use LocalBroadcastReceiver
to communicate with the activity that holds the reference to your WebView
, from there you can properly clear the cache like you were trying to do in the receiver. mWebview.clearCache(true);
Solution 2
In the onReceive
of your BroadcastReceiver you can store a preference in the SharedPreferences
telling that an updated has occurred.
@Override
public void onReceive(Context con, Intent intent)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("hasAppUpdated", true).apply();
}
And then in your Activity
that deals with the WebView
you can verify if there was an App update and clear the cache of the WebView
.
To access the value from the SharedPreferences
:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean hasAppUpdated = prefs.getBoolean("hasAppUpdated", false);
if(hasAppUpdated) {
prefs.edit().putBoolean("hasAppUpdated", false).apply();
mWebview.clearCache(true);
}
The false
value is the default value for the hasAppUpdated
, and after getting the value from the preferences you might want to make the preference back to false again.
Ugly solution
Another way of doing that might be this ugly hack that I don't recommend.
I didn't test this but if you are using WebView
then the cache folder of your app probably contain a folder org.chromium.android_webview
(for versions bellow API 19 this might not be true since the WebView
back then didn't use chromium) and inside that folder you have the cache of your WebView
so if you remove the files inside this folder using the java.io.File
API you probably have your problem solved without requiring accessing the WebView
. Remember that this is a super ugly solution(if it even works) and is far from being a bullet-proof solution.
add a comment |
The context
you are using to find your WebView
reference by findViewById
is the context
from the Receiver
and not a context
from an Activity
therefor it can't access the WebView
There are a lot of possibilities:
Solution 1
You can use LocalBroadcastReceiver
to communicate with the activity that holds the reference to your WebView
, from there you can properly clear the cache like you were trying to do in the receiver. mWebview.clearCache(true);
Solution 2
In the onReceive
of your BroadcastReceiver you can store a preference in the SharedPreferences
telling that an updated has occurred.
@Override
public void onReceive(Context con, Intent intent)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("hasAppUpdated", true).apply();
}
And then in your Activity
that deals with the WebView
you can verify if there was an App update and clear the cache of the WebView
.
To access the value from the SharedPreferences
:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean hasAppUpdated = prefs.getBoolean("hasAppUpdated", false);
if(hasAppUpdated) {
prefs.edit().putBoolean("hasAppUpdated", false).apply();
mWebview.clearCache(true);
}
The false
value is the default value for the hasAppUpdated
, and after getting the value from the preferences you might want to make the preference back to false again.
Ugly solution
Another way of doing that might be this ugly hack that I don't recommend.
I didn't test this but if you are using WebView
then the cache folder of your app probably contain a folder org.chromium.android_webview
(for versions bellow API 19 this might not be true since the WebView
back then didn't use chromium) and inside that folder you have the cache of your WebView
so if you remove the files inside this folder using the java.io.File
API you probably have your problem solved without requiring accessing the WebView
. Remember that this is a super ugly solution(if it even works) and is far from being a bullet-proof solution.
add a comment |
The context
you are using to find your WebView
reference by findViewById
is the context
from the Receiver
and not a context
from an Activity
therefor it can't access the WebView
There are a lot of possibilities:
Solution 1
You can use LocalBroadcastReceiver
to communicate with the activity that holds the reference to your WebView
, from there you can properly clear the cache like you were trying to do in the receiver. mWebview.clearCache(true);
Solution 2
In the onReceive
of your BroadcastReceiver you can store a preference in the SharedPreferences
telling that an updated has occurred.
@Override
public void onReceive(Context con, Intent intent)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("hasAppUpdated", true).apply();
}
And then in your Activity
that deals with the WebView
you can verify if there was an App update and clear the cache of the WebView
.
To access the value from the SharedPreferences
:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean hasAppUpdated = prefs.getBoolean("hasAppUpdated", false);
if(hasAppUpdated) {
prefs.edit().putBoolean("hasAppUpdated", false).apply();
mWebview.clearCache(true);
}
The false
value is the default value for the hasAppUpdated
, and after getting the value from the preferences you might want to make the preference back to false again.
Ugly solution
Another way of doing that might be this ugly hack that I don't recommend.
I didn't test this but if you are using WebView
then the cache folder of your app probably contain a folder org.chromium.android_webview
(for versions bellow API 19 this might not be true since the WebView
back then didn't use chromium) and inside that folder you have the cache of your WebView
so if you remove the files inside this folder using the java.io.File
API you probably have your problem solved without requiring accessing the WebView
. Remember that this is a super ugly solution(if it even works) and is far from being a bullet-proof solution.
The context
you are using to find your WebView
reference by findViewById
is the context
from the Receiver
and not a context
from an Activity
therefor it can't access the WebView
There are a lot of possibilities:
Solution 1
You can use LocalBroadcastReceiver
to communicate with the activity that holds the reference to your WebView
, from there you can properly clear the cache like you were trying to do in the receiver. mWebview.clearCache(true);
Solution 2
In the onReceive
of your BroadcastReceiver you can store a preference in the SharedPreferences
telling that an updated has occurred.
@Override
public void onReceive(Context con, Intent intent)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("hasAppUpdated", true).apply();
}
And then in your Activity
that deals with the WebView
you can verify if there was an App update and clear the cache of the WebView
.
To access the value from the SharedPreferences
:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean hasAppUpdated = prefs.getBoolean("hasAppUpdated", false);
if(hasAppUpdated) {
prefs.edit().putBoolean("hasAppUpdated", false).apply();
mWebview.clearCache(true);
}
The false
value is the default value for the hasAppUpdated
, and after getting the value from the preferences you might want to make the preference back to false again.
Ugly solution
Another way of doing that might be this ugly hack that I don't recommend.
I didn't test this but if you are using WebView
then the cache folder of your app probably contain a folder org.chromium.android_webview
(for versions bellow API 19 this might not be true since the WebView
back then didn't use chromium) and inside that folder you have the cache of your WebView
so if you remove the files inside this folder using the java.io.File
API you probably have your problem solved without requiring accessing the WebView
. Remember that this is a super ugly solution(if it even works) and is far from being a bullet-proof solution.
edited Jan 3 at 16:54
answered Jan 3 at 15:25


João ZãoJoão Zão
10019
10019
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023956%2fclear-webview-cache-in-broadcast-receiver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown