首先要先設定自訂的 WebViewClient 給 WebView
然後在 WebViewClient 去判斷連結網頁還是 mail 或 phone number 去做不同的處理
來看 code:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
myWebView = (WebView)findViewById(R.id.activity_webview);
myWebView.setWebViewClient(myWebViewClient);
myWebView.loadUrl("xxx");
}
WebViewClient myWebViewClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url) {
if (url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}else if (url.startsWith("mailto:")){
MailTo mt = MailTo.parse(url);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { mt.getTo()});
intent.putExtra(Intent.EXTRA_TEXT, mt.getBody());
intent.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject());
intent.putExtra(Intent.EXTRA_CC, mt.getCc());
intent.setType("message/rfc822");
startActivity(i);
view.reload();
return true;
}
else
view.loadUrl(url);
return true;
}
};
留言列表