Issue
My Android app is upgrade to API 29, so we need to change from Xwalkcorelibrary to WebView, when run app can show HTML5 but the content of this page is not show.
When use WebView:
setContentView(R.layout.bookshelf_view);
mWebView = findViewById(R.id.web_view);
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
try {
WebSettings webSettings = mWebView.getSettings();
webSettings.setUserAgentString(webSettings.getUserAgentString() + " " + AndroidCore.getInstance().getApiVersion());
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
mWebView.loadUrl("file:///android_asset/HTML5/index.html");
When use Xwalkcorelibrary :
try {
Method getBridge = XWalkView.class.getDeclaredMethod("getBridge");
getBridge.setAccessible(true);
XWalkViewBridge xWalkViewBridge = null;
xWalkViewBridge = (XWalkViewBridge) getBridge.invoke(mXwalkView);
XWalkSettings webSettings = xWalkViewBridge.getSettings();
webSettings.setUserAgentString(
webSettings.getUserAgentString() + " " + AndroidCore.getInstance().getApiVersion());
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
mXwalkView.setUIClient(new MyUIClient(mXwalkView));
this.configureWebView();
mXwalkView.loadAppFromManifest("file:///android_asset/manifest.json", null)
So what I did wrong ? Please help me figure out solution ?
Solution
Have you tried like this It is working for me in my project :
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("URL");
Intercepting WebView HTTP Requests
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.indexOf("jenkov.com") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if(url.startsWith("http://tutorials.jenkov.com/images/logo.png")){
String mimeType = "image/png";
String encoding = "";
InputStream input = ...;
WebResourceResponse response =
new WebResourceResponse(mimeType, encoding, input);
return response;
}
return null;
}
}
Caching Values in HTML 5 Local Storage
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
Answered By - bugfreerammohan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.