Webview doesn't clear history, cache etc - android

I have an android application that has native framework and content itself is presented in web format and in webview. The meaning of the application is to allow users to use the device using predefined
services that may require autentication.
How ever when I try to clean up the webview caches after user has completed his/her tasks the webview will remember everything and all e.g. login credentials are in place, history remains etc.
I have tried the following to do the clean up without any success, what I am missing in this ?
(wvfo if the overlay fragment in which the webview is that each service is using)
wvfo.getWebView().clearCache(true);
wvfo.getWebView().clearFormData();
wvfo.getWebView().clearHistory();
wvfo.getWebView().clearMatches();
// wvfo.getWebView().setWebViewClient(new WebViewClient());
// wvfo.loadUrl("javascript:document.open();document.close();");
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
wvfo.destroyWebview();
Any ideas what is wrong with this and why the history doesn't get cleared ?
Thanks in advance

Yes, You have to delete webview default DB also. Check the below code.
static void clearWebViewAllCache(Context context, WebView webView) {
try {
AgentWebConfig.removeAllCookies(null);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
context.deleteDatabase("webviewCache.db");
context.deleteDatabase("webview.db");
webView.clearCache(true);
webView.clearHistory();
webView.clearFormData();
clearCacheFolder(new File(AgentWebConfig.getCachePath(context)), 0);
} catch (Exception ignore) {
//ignore.printStackTrace();
if (AgentWebConfig.DEBUG) {
ignore.printStackTrace();
}
}
}
I hope, this will help you.
Happy codding...

Related

android WebView cookies expires when exit app

As the CookieSyncManager.getInstance().sync(); is deprecated Itried to maintain cookies forever in my application using new command
flush() :
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().acceptCookie();
CookieManager.getInstance().acceptThirdPartyCookies(webview);
CookieManager.getInstance().flush();
}
// and more settings for webview
}
But every time I open the app it seems that previous cookies were expired. Do those options help preserving cookies? And Should I put them in onPageFinished?
Besides I have to say that the cookies are working fine on the target website and are set to live for 100 days. Also minSdkVersion is 21 and targetSdkVersion is 29.
Using PersistentCookieJar — persistent and good for encapsulating the Cookies within the App itself. please checkthis
every time APP plan to launch Webview, the cookies will need to be copied from the PersistentCookieJar to the CookieManager.

unlock HTML files

I've an app which is basically a book, written in HTML deployed with phonegap to Apple Store and Google PlayStore.
I will soon updating my app in the store but I would like to officer "in-app purchase" for some of the sections of the book.
I've gone through several plugins in the market but could not figure out how to unlock the pages. The procedure in my app should be,
User taps on section
Section prompts a text to request payment
User completes payment (either consumable/annual subscription or non-consumable/life time)
User able to read rest of the HTML pages in the app.
I really much appreciate your comments/guidance to help. Thank you!
you can check your url in the webview WebViewClient.
so if you want to block page then show inapp purchase dialogue else you can show that page
wb_webview = findViewById(R.id.wb_webview);
wb_webview.setWebViewClient(new web_client());
WebSettings settings = wb_webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
private class web_client extends WebViewClient {
private web_client() {
}
public boolean shouldOverrideUrlLoading(WebView webView, String str) {
//check page you want to show
if(show){
wb_webview.loadUrl(str);
}else{
//inapp purchase dialaug display
}
return true;
}
}
may be it`s work

Android WebView with AngularJS application route changes not being detected in shouldOverrideUrlLoading

I have been working on a hybrid android application. Currently a WebView in our application is pointing to an AngularJS 1.5.7 application. When the user hits a button inside of the application that changes the route I was expecting the shouldOverrideUrlLoading function to be called inside of my WebViewClient. However, this is not the case. It looks like shouldOverrideUrlLoading does not get hit on Angualar route changes.
This being the case I have gone down the following rabbit holes:
onPageFinished - Overriding this function in the WebViewClient works, however, it is not being called until after the new route is getting hit. Which is adding to the application loading time and creating a choppy experience. ` #Override
public void onPageFinished(WebView view, String url) {
if (url.endsWith("/#/")) {
signOut();
} else if (url.endsWith("/login")) {
// TODO: show some sort of failure message?
Log.i("Login Route", "The webview just attempted to go to the login route.");
signOut();
} else if (url.endsWith("/security")) {
Intent intent = new Intent(getApplicationContext(), SecurityActivity.class);
startActivity(intent);
}
}`
shouldInterceptRequest - Overriding this function allows you to watch for requests. However, by the time the requests go out from the AngularJS application the web view is showing a new route once again providing a choppy user experience.
onLoadResource - same
JavaScriptInterface - Currently I have set up a JavaScript interface to watch for window.location changes. This seems to catch the route changes quicker than any of the above options, however, there is still a glimpse quick flicker of the web page I do not want to do go to. You can find how to do Javascript bridging on this post
Any suggestions would be greatly appreciated! Thanks.

Android WebView performance

In my app, I am loading a list of external url's in webview and allow user to flip through them. Webviews are loaded on to a view flipper. I find the performance is really bad in webview load url. I have tried everything from using the frame layout to limiting the number of webviews to load. Still the performance is not satisfactory.
How do I optimize the performance of webview? This should be a common usage. Am I missing something obvious.
My Webview settings are -
webView.setInitialScale(WEBVIEW_SCALE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.setWebViewClient(new MyWebViewClient());
webView.setOnTouchListener( new OnTouchListener());
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
I think the following works best:
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Android 19 has Chromium engine for WebView. I guess it works better with hardware acceleration.
For more info Android 4.4 KitKat, the browser and the Chrome WebView
This has already been discussed here: Enhance webView performance (should be the same performance as native Web Browser)
I ran into a similar issue, and after some heavy debugging noticed the
native browser and WebView browser seem to be using different caches.
This code can be used to disable the WebView cache, and made WebView
much faster for me (though at the expense of not caching). Note that
it uses private APIs, so by using it you're risking the code will
break in future releases:
try
{
Method m = CacheManager.class.getDeclaredMethod("setCacheDisabled", boolean.class);
m.setAccessible(true);
m.invoke(null, true);
}
catch (Throwable e)
{
Log.i("myapp","Reflection failed", e);
}
Improvise Answer:
The above Solution mentioned CacheManager.class is not supported.
try {
val m: Method = ServiceWorkerWebSettingsCompat.CacheMode::class.java.getDeclaredMethod(
"setCacheDisabled",
Boolean::class.javaPrimitiveType
)
m.isAccessible = true
m.invoke(null, true)
} catch (e: Throwable) {
Log.i("myapp", "Reflection failed", e)
}

Loading a Maps API site in a Native Android Application

I'm trying to follow the example from google:
http://code.google.com/apis/maps/articles/android_v3.html
Using the example files from their SVN repo:
(http)gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/
But although it seems to compile and export it fails; can someone sanity check that its not just me this fails for and any hints as to if its a quick thing to fix, I've been prodding it with try/catch for the last 2 hours to no avail.
Thanks :o)
Forgot to add
It Compiles and Uploads to the emulator (and to my phone) but running it just results in
The Application WebMapActivity (process com.google.android.examples.webmap)
has stopped unexpectedly. Please try again. [Force Close]
Filtered it down to
private void setupWebView() {
/*
final String centerURL = "javascript:centerAt("
+ mostRecentLocation.getLatitude() + ","
+ mostRecentLocation.getLongitude() + ")";
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
// Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl(centerURL);
}
});*/
webView = (WebView) findViewById(R.id.webview);
webView.loadUrl(MAP_URL);
}
Bit easier to filter out the actual issue when you know which bit doesnt work thanks to CommonsWare for the great book, poking through that and test-code that ACTUALLY WORKS made it a bit easier to work out what was going on. And thanks for the debug info :o)

Categories

Resources