Help us understand the problem. What is going on with this article?

Webview内でtel: mailto: リンクに遷移する時の対応

More than 5 years have passed since last update.

Webview内でtel:とmailto:リンクに飛ぼうとするとエラーになる。。。

http://www.tagindex.com/html5/text/a_tel.html
http://www.tagindex.com/html_tag/link/a_mailto_sub.html
本来このリンクは、電話をかけるたりメールを出したりするリンクである。

しかしAndroid Webview内でリンクを踏むとエラー画面に・・・
そこで tel: と mailto: リンクを踏んだ際は、WebViewClientのshouldOverrideUrlLoadingで検知してIntentに変換してみる。

tel:の時は発信しますか?のダイアログを付けてみた版

public class MainActivity extends FragmentActivity {    
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    // 電話発信ダイアログ
                    TelDialogFragment dialogFragment = TelDialogFragment.newInstance(url);
                    dialogFragment.show(MainActivity.this.getSupportFragmentManager(), "tel_dialog");
                    return true;
                }else if(url.startsWith("mailto:")){
                    Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                return true;
            }
        });
        mWebView.loadUrl("http://");;
    }

    // 電話確認ダイアログ
    public static class TelDialogFragment extends DialogFragment {
        public static TelDialogFragment newInstance(String telNumber){
            TelDialogFragment frag = new TelDialogFragment();
            Bundle args = new Bundle();
            args.putString("tel_number", telNumber);
            frag.setArguments(args);
            return frag;
        }
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("確認");
            builder.setMessage("発信しますか?");
            builder.setPositiveButton("はい",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String telNumber = (String) getArguments().get("tel_number");
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(telNumber));
                    startActivity(intent);
                }
            });
            builder.setNegativeButton("いいえ",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {}
            });
            return builder.create();
        }
    }
}
ueno-yuhei
水樹奈々が好き!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away