简体   繁体   中英

Clear webview cache in broadcast receiver?

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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM