在webview中打开拨号器和打开Web链接[错误] ANDROID(Open Dialer AND Open web links in webview[Error] ANDROID) 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应该抓住两个web链接在webview中打开它们,如果我没有误读并用android拨号器打开电话号码。 使用上面的代码,我可以获得在webview中打开的链接,但它不会打开拨号器中的数字。 如果我将此代码添加到程序中。

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

我可以打开拨号器,但Web链接不会在webview中打开,但实际上会在默认浏览器中打开。 所以我想要一些帮助,如何让它在网页浏览器中的拨号器和网络链接中打开电话号码,而不是只有或。

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; } }

So i have the above source code the function shoudOverrideUrlLoading should catch both the web links to open them in the webview if I didn't misread and open phone numbers with the android dialer. with the above code i can get links to open in the webview but it wont open the numbers in the dialer. If i add this code to the program.

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

I can get the dialer to open but then the web links wont open in the webview but will actually open in the default browser. So i would like some help in how to get it to open phone numbers in the dialer and web links in the webview and not just have either or.

最满意答案

用这个

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()的所有其他用法

编辑:这将解决!

Use this

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; }

}

And set the web view client as

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

This will make sure the links are opened in the same web view, and not the browser. Note : Remove all other uses of setWebViewClient()

Edited : This will solve!

在webview中打开拨号器和打开Web链接[错误] ANDROID(Open Dialer AND Open web links in webview[Error] ANDROID) 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应该抓住两个web链接在webview中打开它们,如果我没有误读并用android拨号器打开电话号码。 使用上面的代码,我可以获得在webview中打开的链接,但它不会打开拨号器中的数字。 如果我将此代码添加到程序中。

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

我可以打开拨号器,但Web链接不会在webview中打开,但实际上会在默认浏览器中打开。 所以我想要一些帮助,如何让它在网页浏览器中的拨号器和网络链接中打开电话号码,而不是只有或。

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; } }

So i have the above source code the function shoudOverrideUrlLoading should catch both the web links to open them in the webview if I didn't misread and open phone numbers with the android dialer. with the above code i can get links to open in the webview but it wont open the numbers in the dialer. If i add this code to the program.

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

I can get the dialer to open but then the web links wont open in the webview but will actually open in the default browser. So i would like some help in how to get it to open phone numbers in the dialer and web links in the webview and not just have either or.

最满意答案

用这个

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()的所有其他用法

编辑:这将解决!

Use this

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; }

}

And set the web view client as

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

This will make sure the links are opened in the same web view, and not the browser. Note : Remove all other uses of setWebViewClient()

Edited : This will solve!