2011-12-20 38 views
7

我是Android開發人員的新手,並且此webview和webview客戶端正在殺死我。這是我的情景:由於默認瀏覽器,Android無法在webview中實施Facebook評論

  1. 我需要加載一個網頁,包含Facebook的社交插件(用於評論特定的網址),我使用的WebView它
  2. 當用戶使用Facebook的點擊評論,他/她是被賦予登錄頁面上相同的WebView(而不是打開默認的瀏覽器)
  3. 一旦登錄成功,第一頁(包含社交插件的)是要顯示,允許用戶發表評論

我所要做的就是模仿眉毛的工作過程即用戶在登錄時,他/她被自動授予添加Facebook評論的權限。

我的問題:

我不知道如何從瀏覽器中得到的所有驗證和重定向回我的應用程序的WebView。我想要的是在我的應用程序Web視圖中執行所有過程,而不是使用默認瀏覽器。

我已經檢查了所有堆棧溢出問題,其中大多數人建議使用Facebook連接和Facebook android SDK等開源Facebook插件。此外,我得到了一些關於CookieManagerCookieSyncManagerWebViewClient,WebChromeClient的信息,但我無法執行我的問題。而且我發現最接近的是:如果

How to handle facebook like with confirm in android webview

所以鄉親,你可以在正確的方向指向我,我會非常高興。我仍然試圖瞭解如何在webview上執行所有操作,如果發生任何問題,我一定會發布。

在此先感謝

更新

我只能實現facebook登錄,但無法實現AOLHotmailYahoo登錄。對於facebook登錄剛剛創建自定義WebViewClient和方法shouldOverrideUrlLoading

if(url.contains("https://www.facebook.com/connect/window_comm.php")){ 
    webView.clearHistory(); 
    webView.loadUrl(remoteUrl); 
} 
return false; 

要允許多種登錄下面的技術我已經實現,但它不工作

if(url.contains("https://www.facebook.com/connect/window_comm.php")){ 
    String cookieString = cookieManager.getCookie("facebook.com"); 
    if(cookieString != null){ 
     cookieManager.setCookie("remoteUrldomain.com", cookieString); 
     CookieSyncManager.getInstance().sync(); 
     webView.clearHistory(); 
     webView.loadUrl(remoteUrl); 
    } 
} 
return false; 

我還是盡我所能找到解決方案,那裏的任何人都會引導我以正確的方向感謝。 在此先感謝

回答

11

提供 How to handle facebook like with confirm in android webview 答案是最好的解決辦法我發現這樣far.What我瞭解到的情況是AndroidWebView默認不加載的HTML視圖彈出爲此,我們需要使用WebChromeClient它處理所有這些。看看下面的代碼

private String requestUrl="some web link containing facebook social comment"; 
private WebView webView,childView =null; 
private LinearLayout parentLayout; 
private Activity MyActivity; 
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 

    setContentView(R.layout.main); 

    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON); 
    parentLayout =(LinearLayout)findViewById(R.id.parentLayout); 


    MyActivity = this; 


    webView = new WebView(this); 
    webView.setLayoutParams(getLayoutParams()); 

    webView.setWebViewClient(new FaceBookClient()); 
    webView.setWebChromeClient(new MyChromeClient()); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setAppCacheEnabled(true); 
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    webView.getSettings().setSupportMultipleWindows(true); 
    webView.getSettings().setSupportZoom(true); 
    webView.getSettings().setBuiltInZoomControls(true); 

    parentLayout.addView(webView); 
    webView.loadUrl(requestUrl); 

} 

    private LinearLayout.LayoutParams getLayoutParams(){ 
    return new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
} 


    final class MyChromeClient extends WebChromeClient{ 
    @Override 
    public boolean onCreateWindow(WebView view, boolean dialog, 
      boolean userGesture, Message resultMsg) { 
     childView = new WebView(SmCommentTestActivity.this); 
     childView.getSettings().setJavaScriptEnabled(true); 
     childView.getSettings().setSupportZoom(true); 
     childView.getSettings().setBuiltInZoomControls(true); 
     childView.setWebViewClient(new FaceBookClient()); 
     childView.setWebChromeClient(this); 
     childView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 


     parentLayout.addView(childView); 


     childView.requestFocus(); 
     webView.setVisibility(View.GONE); 

      /*I think this is the main part which handles all the log in session*/ 
     WebView.WebViewTransport transport =(WebView.WebViewTransport)resultMsg.obj; 
     transport.setWebView(childView); 
     resultMsg.sendToTarget(); 
     return true; 
    } 


    @Override 
    public void onProgressChanged(WebView view, int newProgress) { 
     MyActivity.setProgress(newProgress*100); 
    } 

    @Override 
    public void onCloseWindow(WebView window) { 
     parentLayout.removeViewAt(parentLayout.getChildCount()-1); 
     childView =null; 
     webView.setVisibility(View.VISIBLE); 
     webView.requestFocus(); 
    } 
} 

    private class FaceBookClient extends WebViewClient{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     Log.i("REQUEST URL",url); 
     return false; 
    } 
} 

    @Override 
    public void onBackPressed() { 
    if(childView != null && parentLayout.getChildCount()==2){ 
     childView.stopLoading(); 
     parentLayout.removeViewAt(parentLayout.getChildCount()-1); 
     if(webView.getVisibility() == View.GONE) 
      webView.setVisibility(View.VISIBLE); 
    }else{   
     super.onBackPressed(); 
    } 
} 

這是所有一個必須做落實Android WebViewFacebook Social Comment Plugin,如果有人發現在它的任何瑕疵的話,我會很樂意更正it.And希望,該解決方案將節省和時間在像我這樣的困擾的開發者;)

+0

這個代碼中應該requestUrl應該是什麼.. – 2014-11-04 09:48:24

+0

我需要安裝任何jar到lib文件夾嗎? – 2015-02-02 10:02:55

+0

我登錄後不斷刷新頁面。 – Minion 2016-07-02 06:07:15

相關問題