Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

(android) WebViewClient does not work when url is redirected

I am facing problem when url is redirected. I set webviewclient and It is supposed to finish webview activity when url is redirected. But it is not working and I wonder why... below is my code and I would be appreciated if anyone can help me. thx.

public class WebViewActivity extends Activity {

WebView mWebView;
String fbRegsitered = "http://m.facebook.com/gettingstarted/";
//"http://m.facebook.com/r.php?refid=0
String fbUnregisterd = "http://m.facebook.com/r.php?refid=0";
private ProgressDialog mSpinner;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    mWebView = (WebView) findViewById(R.id.webview); 
    mWebView.getSettings().setJavaScriptEnabled(true);  
    mWebView.loadUrl("http://m.facebook.com/r.php");  
    mWebView.setWebViewClient(new HelloWebViewClient()); 
}

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
        mWebView.goBack(); 
        return true; 
    } 
    return super.onKeyDown(keyCode, event); 

}

private class HelloWebViewClient extends WebViewClient { 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        //view.loadUrl(url); 
        if(url.contains(fbRegsitered)){
            WebViewActivity.this.finish();
        }
        if(url.startsWith(fbUnregisterd)){              
            //finish();
        }
        return true; 
    } 
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I think its because your this condition,

if(url.contains(fbRegsitered)){
            WebViewActivity.this.finish();
        }

is not satisfying so its not calling WebViewActivity.this.finish();. The reason is you are loading url - "http://m.facebook.com/r.php" and in the condition you are checking for "http://m.facebook.com/gettingstarted/". So, its returning false where there is not else part to handle the else part.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...