2012-03-06 78 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