Android 如何在同一webview应用程序中打开两个不同的url
我正在开发一个android电子商务应用程序,我使用webview将响应网站调用到移动应用程序中,每个功能都可以正常工作,但当我单击PaynLine时,它会在浏览器中打开支付网关页面。如何在同一应用程序中隐藏浏览器并打开支付网关页面 MainActivity.javaAndroid 如何在同一webview应用程序中打开两个不同的url,android,webview,Android,Webview,我正在开发一个android电子商务应用程序,我使用webview将响应网站调用到移动应用程序中,每个功能都可以正常工作,但当我单击PaynLine时,它会在浏览器中打开支付网关页面。如何在同一应用程序中隐藏浏览器并打开支付网关页面 MainActivity.java package com.prashantlaldas; import android.app.Activity; import android.os.Bundle; import
package com.prashantlaldas;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
public WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
mWebView.getSettings().setUseWideViewPort(false);
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com/");
mWebView.setWebViewClient(new MyAppWebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// mWebView.loadUrl("file:///android_asset/error.html");
String data = "\n" +
"\n" +
"<div id=\"container\">\n" +
"\n" +
"<div class=\"connection-problem\">\n" +
"<p><img src=\"noconnection.png\" width=\"100px\" height=\"69px\"></p>\n" +
"\n" +
"<p>No internet connection.</br>Please Turn ON your data or wifi</p>\n" +
"<a href=\"http://www.google.com/ class=\"button\" style=\"background-color: #fff;border: none;color: #de1616;padding: 8px 22px;text-align: center;border-radius: 4px;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;border: 1px solid #de1616;\">Retry</a>\n" +
"</div>\n" +
"</div>\n" +
"<style>\n" +
".connection-problem{text-align: center;margin-top: 50%;}\n" +
"#container {\n" +
" display: flex; /* establish flex container */\n" +
" flex-direction: column; /* stack flex items vertically */\n" +
" justify-content: center; /* center items vertically, in this case */\n" +
" align-items: center; /* center items horizontally, in this case */\n" +
" height: 300px; \n" +
"\n" +
"}\n" +
" \n" +
" \n" +
" \n" +
"\n" +
"</style>";
mWebView.loadDataWithBaseURL("file:///android_asset/error.html", data, "text/html", "utf-8", null);
}
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
}
);
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
finish();
}
}
}
看看这个
public class MainActivity extends AppCompatActivity {
private WebView mWebView = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
mWebView.setWebViewClient(new myWebClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// load URL here
mWebView.loadUrl("http://www.google.com/");
}
/**
* The type My web client.
*/
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// page started event
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onPageFinished(final WebView view, final String url) {
super.onPageFinished(view, url);
new Thread(new Runnable() {
@Override
public void run() {
// hide progress bar
}
}).start();
}
}
}
假设你在印度。我用你的代码重现了这个问题。它在外部浏览器中加载google.co.in.yes I an from indiaString host=Uri.parse(url.getHost();如果(host.contains(“www.google.co.in”){return false;}使用contains代替endwith@PrashantDas我很高兴能帮上忙
public class MainActivity extends AppCompatActivity {
private WebView mWebView = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
mWebView.setWebViewClient(new myWebClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// load URL here
mWebView.loadUrl("http://www.google.com/");
}
/**
* The type My web client.
*/
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// page started event
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onPageFinished(final WebView view, final String url) {
super.onPageFinished(view, url);
new Thread(new Runnable() {
@Override
public void run() {
// hide progress bar
}
}).start();
}
}
}