我不知道我当时到底做了什么,但问题已经解决了。我给你代码。尝试找出:)
WebView browser,mWebviewPop;
private void open(){
browser = (WebView)findViewById(R.id.webView1);
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setAppCacheEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setSupportMultipleWindows(true);
browser.setWebViewClient(new MyBrowser());
browser.setWebChromeClient(new MyCustomChromeClient());
mContext=this.getApplicationContext();
browser.loadUrl(target_url);
MainActivity.this.progressBar.setProgress(0);
browser.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
private class MyBrowser extends WebViewClient {
String redirectUrl = "MY URL";
private void noInternet() {
WebView webview = (WebView) findViewById(R.id.webView1);
RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
tryAgainLayout.setVisibility(View.VISIBLE);
webview.setVisibility(View.GONE);
webview.destroy();
progressLayout.setVisibility(View.INVISIBLE);
}
public void visible(){
WebView webview = (WebView) findViewById(R.id.webView1);
RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
tryAgainLayout.setVisibility(View.INVISIBLE);
webview.setVisibility(View.INVISIBLE);
progressLayout.setVisibility(View.VISIBLE);
}
public void unvisible(){
WebView webview = (WebView) findViewById(R.id.webView1);
RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
tryAgainLayout.setVisibility(View.INVISIBLE);
webview.setVisibility(View.VISIBLE);
progressLayout.setVisibility(View.INVISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
if (host.equals(target_url_prefix))
{
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
baseLayout.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com"))
{
return false;
}
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
noInternet();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
visible();
}
@Override
public void onPageFinished(WebView view, String url) {
unvisible();
System.out.println("\n" +view.getUrl());
if(url.startsWith("https://m.facebook.com/v2.1/dialog/oauth")){
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
baseLayout.removeView(mWebviewPop);
mWebviewPop=null;
}
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
private class MyCustomChromeClient extends WebChromeClient
{
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new MyBrowser());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
baseLayout.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
@Override
public void onCloseWindow(WebView window) {
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}