標籤:android style blog http color os sp for on
由於項目需求製做路由器的登入,有的路由可以直接開啟網頁登入,有的是開啟的登入窗
我們的路由是開啟的登入視窗,我在自己寫的時候用WebView打不開登入窗
在網上搜尋的答案是WebView.setHttpAuthUsernamePassword(host, realm, username, password)WebView增加WebChromeClient重寫onJsAlert發現以上辦法都不能解決問題 用瀏覽器開啟的時候會彈出登入框,當我用Android WebView登入的時候提示登入失敗打不開登入框,一開始我以為是js產生的彈出框實驗給WebView setWebChromeClient 重寫 WebChromeClient 中的
public boolean onJsAlert(WebView view, String url, String message, JsResult result) public boolean onJsConfirm(WebView view, String url, String message, JsResult result)public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result)public boolean onJsTimeout()
發現以上方法沒有一個執行到瞭然後我在對比Firefox,IE,Google Chrome感覺是瀏覽器自己做的瀏覽器開啟的時候會提示輸入使用者名稱和密碼 發現WebViewClient中有一個方法 onReceivedHttpAuthRequest然後查看源碼
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { handler.cancel();}發現是直接調用的 handler.cancel();給WebView增加WebViewClient 重寫 onReceivedHttpAuthRequest 將此方法置空發現登入的時候會停止在一個空白頁面,等待處理,並沒有像開始直接開啟的時候提示登入失敗 然後查看onReceivedHttpAuthRequest中的參數有一個HttpAuthHandler 源碼預設是調用的cancel() 發現還有一個方法 proceed(String username, String password) 然後在重寫 onReceivedHttpAuthRequest時
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { handler.proceed("admin", "admin");} // PS: admin 是我路由器預設的帳號和密碼
然後就登入進去了我的路由器,問題到這裡已經解決
onReceivedHttpAuthRequest這個方法也是在主線程中的,可以在這裡製做自己的登入視窗
Android-WebView路由登入192.168.1.1