2012-03-06 86 views
1
package com.example.t2noob; 


import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 
import android.widget.Toast; 

public class Activity extends Activity 
{ 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (url.startsWith("tel:")) { 
      Intent intent = new Intent(Intent.ACTION_DIAL, 
        Uri.parse(url)); 
      startActivity(intent); 
    }else if(url.startsWith("http:") || url.startsWith("https:")) { 
     view.loadUrl(url); 
    } 
    return true; 
    } 



    WebView mWebView; 




    public void onCreate(Bundle paramBundle) 
    { 



     super.onCreate(paramBundle); 
     requestWindowFeature(1); 
     getWindow().setFlags(1024, 1024); 
     setContentView(2130903040); 
     final Button button = (Button) findViewById(R.id.Home);//BUTTONS ON TOP OF WEBVIEW. HOME 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      mWebView.loadUrl("test.com"); 
      } 
     }); 
     final Button button1 = (Button) findViewById(R.id.Back);//BUTTONS ON TOP OF WEBVIEW. BACK 
     button1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      mWebView.goBack(); 
      } 
      }); 

     this.mWebView = ((WebView)findViewById(2131034112)); 
     this.mWebView.getSettings().setJavaScriptEnabled(true); 
     this.mWebView.setWebViewClient(new WebViewClient()); 
     this.mWebView.getSettings().setJavaScriptEnabled(true); 
     this.mWebView.setVerticalScrollBarEnabled(true); 
     this.mWebView.setHorizontalScrollBarEnabled(true); 
     this.mWebView.loadUrl("test.com"); 
     this.mWebView.getSettings().setLoadWithOverviewMode(true); 

     } 


     public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)//LETS USER PUSH BACK BUTTON ON PHONE TO GO BACK A PAGE IN WEBVIEW. 
     { 
     boolean bool; 
     if ((paramInt != 4) || (!this.mWebView.canGoBack())) 
     { 
    bool = super.onKeyDown(paramInt, paramKeyEvent); 
} 
else 
{ 
    this.mWebView.goBack(); 
    bool = true; 
} 
return bool; 
} 

} 

所以我有以上源代碼的功能shoudOverrideUrlLoading 應該抓住這兩個網址鏈接在網頁視圖中打開它們,如果我沒有看錯 和開放電話號碼與android撥號程序。 與上面的代碼我可以得到鏈接打開在web視圖中,但它不會打開撥號程序中的數字。 如果我將此代碼添加到程序中。打開撥號器和web視圖中打開網頁鏈接[錯誤] ANDROID

private static final WebViewClient Webview = null; 
this.mWebView.setWebViewClient(Webview); 

我能撥號器打開,但隨後的網絡鏈接在網頁視圖不會打開,但將在默認瀏覽器實際打開。 所以我想在如何讓它在撥號程序中打開電話號碼和web瀏覽器中的網頁鏈接,而不是隻有或者有一些幫助。

回答

0

使用此

private class HelloWebViewClient extends WebViewClient { 
@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if(url.contains("tel:")) 
    { 
    //make the substring with the telephone number, and invoke intent for dialer 
    } 
else 
    view.loadUrl(url); 
    return true; 
} 

}

,並設置Web視圖客戶端作爲

this.mWebView.setWebViewClient(new HelloWebViewClient()); 

這將確保該鏈接會在同一個Web視圖中打開,而不是瀏覽器。 注意:刪除所有其他用途的setWebViewClient()

編輯:這將解決!

+0

是的我知道這應該修復在web視圖中打開的鏈接,但我想要在web視圖中打開web鏈接和電話號碼,短信分別打開撥號程序和消息傳遞應用程序。與您的代碼,它只會打開在web視圖中的鏈接,但不會打開撥號當數字被點擊(我實際上有你的代碼之前,但當我添加的東西打開撥號時,數字被點擊它不會打開網頁在web視圖中的鏈接。)所以我想知道如果我做錯了什麼。因爲他們不會抓住電話號碼和網絡鏈接。它只是做一個或另一個。 – user1251268 2012-03-06 04:46:56

+0

使用這樣的東西,如果(url.contains(「tel:」)){//創建子字符串,並調用意圖撥號,並在其他地方,使用view.loadUrl(url); } 得到它了? – nithinreddy 2012-03-06 05:22:08

+0

我能弄明白它必須做的方式,我設置我的功能後,我修復了一切正常的功能。謝謝你的幫助。 – user1251268 2012-03-13 02:51:16