use of android.webkit.WebViewClient in project MyApp by MatthewDevelop.
the class WebViewPresenter method setWebView.
public void setWebView(WebView webView, String url) {
final CommonWebView commonWebView = getView();
final ProgressBar progressBar = getView().getProgressBar();
// TODO WebView的设置有待学习
WebSettings webSettings = webView.getSettings();
// 设置JavaScript可用
webSettings.setJavaScriptEnabled(true);
// 设置屏幕适应
// 设置支持缩放,是下项设置的前提
webSettings.setSupportZoom(true);
// 设置可以缩放缩放
webSettings.setBuiltInZoomControls(true);
// 设置隐藏原生缩放控件
webSettings.setDisplayZoomControls(false);
// 设置调整图片至合适webView大小
webSettings.setUseWideViewPort(true);
// 设置缩放至屏幕大小
webSettings.setLoadWithOverviewMode(true);
// 支持通过js打开新的窗口
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// 设置默认编码格式
webSettings.setDefaultTextEncodingName("utf-8");
// 设置自动加载图片
webSettings.setLoadsImagesAutomatically(true);
// 设置支持内容重新布局
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
// 开启Application Cache(html5缓存)功能
webSettings.setAppCacheEnabled(true);
// 开启Dom Storage功能
webSettings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient() {
String startUrl;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
startUrl = url;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
/**
* 跳转链接在此方法中执行
* @param view
* @param url
* @return
*/
// TODO shouldOverrideUrlLoading(WebView view, String url)和 shouldOverrideUrlLoading(WebView view, WebResourceRequest request)区别
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (startUrl != null && url.equals(startUrl)) {
view.loadUrl(url);
} else {
return super.shouldOverrideUrlLoading(view, url);
}
return true;
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
commonWebView.setTitle(title);
}
});
webView.loadUrl(url);
}
use of android.webkit.WebViewClient in project summer-android by cn-cerc.
the class FrmScanProduct method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frm_scan_product);
instance = this;
Intent intent = getIntent();
imgBack = (ImageView) findViewById(R.id.imgBack);
imgBack.setOnClickListener(this);
lblTitle = (TextView) findViewById(R.id.lblTitle);
lblTitle.setText(intent.getStringExtra("title"));
this.homeUrl = intent.getStringExtra("homeUrl");
this.returnUrl = intent.getStringExtra("returnUrl");
this.appendUrl = intent.getStringExtra("appendUrl");
this.modifyUrl = intent.getStringExtra("modifyUrl");
this.deleteUrl = intent.getStringExtra("deleteUrl");
this.viewUrl = intent.getStringExtra("viewUrl");
chkIsSpare = (CheckBox) findViewById(R.id.chkIsSpare);
edtBarcode = (EditText) findViewById(R.id.edtBarcode);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
adapter = new ListViewAdapter(this, R.layout.activity_list_scan_product, dataSet, this);
lstView = (ListView) findViewById(R.id.lstView);
lstView.setAdapter(adapter);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// 返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
view.loadUrl(url);
return true;
}
});
// 启用支持javascript
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
// 打开指定的网页
webView.loadUrl(MyApp.getFormUrl(homeUrl));
// 关闭软键盘
edtBarcode.setInputType(InputType.TYPE_NULL);
edtBarcode.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
saveBarcode();
}
return false;
}
});
// 自动归位焦点
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = MSG_TIMER;
handler.sendMessage(msg);
}
}, 1000, 200);
}
use of android.webkit.WebViewClient in project incubator-weex by apache.
the class WXWebViewTest method testLoadUrl.
@Test
public void testLoadUrl() throws Exception {
String url = "http://www.taobao.com";
view.loadUrl(url);
WebViewClient client = shadow.getWebViewClient();
client.onPageStarted(webView, url, null);
client.onPageFinished(webView, url);
WebChromeClient chromeClient = shadow.getWebChromeClient();
chromeClient.onProgressChanged(webView, 10);
chromeClient.onProgressChanged(webView, 100);
chromeClient.onReceivedTitle(webView, "test");
}
use of android.webkit.WebViewClient in project incubator-weex by apache.
the class WXWebView method initWebView.
private void initWebView(WebView wv) {
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setUseWideViewPort(true);
settings.setDomStorageEnabled(true);
settings.setSupportZoom(false);
settings.setBuiltInZoomControls(false);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
WXLogUtils.v("tag", "onPageOverride " + url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
WXLogUtils.v("tag", "onPageStarted " + url);
if (mOnPageListener != null) {
mOnPageListener.onPageStart(url);
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
WXLogUtils.v("tag", "onPageFinished " + url);
if (mOnPageListener != null) {
mOnPageListener.onPageFinish(url, view.canGoBack(), view.canGoForward());
}
if (mOnMessageListener != null) {
evaluateJS("javascript:(window.postMessage = function(message, targetOrigin) {" + "if (message == null || !targetOrigin) return;" + (DOWNGRADE_JS_INTERFACE ? "prompt('" + BRIDGE_NAME + "://postMessage?message=' + JSON.stringify(message) + '&targetOrigin=' + targetOrigin)" : BRIDGE_NAME + ".postMessage(JSON.stringify(message), targetOrigin);") + "})");
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (mOnErrorListener != null) {
// mOnErrorListener.onError("error", "page error code:" + error.getErrorCode() + ", desc:" + error.getDescription() + ", url:" + request.getUrl());
mOnErrorListener.onError("error", "page error");
}
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
if (mOnErrorListener != null) {
mOnErrorListener.onError("error", "http error");
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
if (mOnErrorListener != null) {
mOnErrorListener.onError("error", "ssl error");
}
}
});
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
showWebView(newProgress == 100);
showProgressBar(newProgress != 100);
WXLogUtils.v("tag", "onPageProgressChanged " + newProgress);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (mOnPageListener != null) {
mOnPageListener.onReceivedTitle(view.getTitle());
}
}
@Override
public boolean onJsPrompt(WebView view, String url, String text, String defaultValue, JsPromptResult result) {
Uri uri = Uri.parse(text);
String scheme = uri.getScheme();
if (TextUtils.equals(scheme, BRIDGE_NAME)) {
if (TextUtils.equals(uri.getAuthority(), "postMessage")) {
String message = uri.getQueryParameter("message");
String targetOrigin = uri.getQueryParameter("targetOrigin");
onMessage(message, targetOrigin);
result.confirm("success");
} else {
result.confirm("fail");
}
return true;
}
return super.onJsPrompt(view, url, text, defaultValue, result);
}
});
if (!DOWNGRADE_JS_INTERFACE) {
wv.addJavascriptInterface(new Object() {
@JavascriptInterface
public void postMessage(String message, String targetOrigin) {
onMessage(message, targetOrigin);
}
}, BRIDGE_NAME);
}
}
use of android.webkit.WebViewClient in project RespawnIRC-Android by FranckRJ.
the class WebBrowserActivity method onCreate.
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webbrowser);
initToolbar(R.id.toolbar_webbrowser);
String cookies = PrefsManager.getString(PrefsManager.StringPref.Names.COOKIES_LIST);
/*L'ancienne COOKIES_LIST contenait deux cookies, la nouvelle n'en contient plus qu'un.*/
if (cookies.contains(";")) {
String firstCookie = cookies.substring(0, cookies.indexOf(";"));
String secondCookie = cookies.substring(cookies.indexOf(";") + 1, cookies.length());
CookieManager.getInstance().setCookie("http://www.jeuxvideo.com/", firstCookie);
CookieManager.getInstance().setCookie("http://www.jeuxvideo.com/", secondCookie);
} else {
CookieManager.getInstance().setCookie("http://www.jeuxvideo.com/", cookies);
}
browserWebView = findViewById(R.id.webview_webbrowser);
browserWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
currentUrl = url;
updateTitleAndSubtitle();
}
});
browserWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
currentTitle = title;
updateTitleAndSubtitle();
}
});
browserWebView.getSettings().setUseWideViewPort(true);
browserWebView.getSettings().setSupportZoom(true);
browserWebView.getSettings().setBuiltInZoomControls(true);
browserWebView.getSettings().setDisplayZoomControls(false);
browserWebView.getSettings().setJavaScriptEnabled(true);
Undeprecator.webSettingsSetSaveFormData(browserWebView.getSettings(), false);
Undeprecator.webSettingsSetSavePassword(browserWebView.getSettings(), false);
currentTitle = getString(R.string.app_name);
if (getIntent() != null && savedInstanceState == null) {
String newUrlToLoad = getIntent().getStringExtra(EXTRA_URL_LOAD);
if (!Utils.stringIsEmptyOrNull(newUrlToLoad)) {
currentUrl = newUrlToLoad;
browserWebView.loadUrl(currentUrl);
}
} else if (savedInstanceState != null) {
currentTitle = savedInstanceState.getString(SAVE_TITLE_FOR_BROWSER, getString(R.string.app_name));
currentUrl = savedInstanceState.getString(SAVE_URL_FOR_BROWSER, "");
if (!currentUrl.isEmpty()) {
browserWebView.loadUrl(currentUrl);
}
}
updateTitleAndSubtitle();
PrefsManager.putInt(PrefsManager.IntPref.Names.NUMBER_OF_WEBVIEW_OPEN_SINCE_CACHE_CLEARED, PrefsManager.getInt(PrefsManager.IntPref.Names.NUMBER_OF_WEBVIEW_OPEN_SINCE_CACHE_CLEARED) + 1);
PrefsManager.applyChanges();
}
Aggregations