LoginSignup
5
4

More than 5 years have passed since last update.

AndroidのWebViewで「LINEで送る」ボタンに対応させる

Posted at

概要

WebViewClient の shouldOverrideUrlLoading の返り値を false にしてアプリ内webviewで回遊させるようにしている場合、「LINEで送る」ボタンをクリックした後に表示される以下のような画面で「LINEアプリで開く」のボタンがそのままだと動かないのでその対応方法です。

対応方針

「LINEアプリで開く」のボタンのリンクURLはSchemeが intent:// になっています。
なので、shouldOverrideUrlLoadingで受け取ったURLのSchemeがintentだった場合に、URLからINTENTインスタンスを作るようにします。

下記のstackoverflowの記事を参考にしています。
http://stackoverflow.com/questions/33151246/how-to-handle-intent-on-a-webview-url

実装例

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if ("intent".equals(request.getUrl().getScheme())) {
                    onLoadIntentSchemeUrl(request);
                    return true;
                }
                return false;
            }

            private void onLoadIntentSchemeUrl(WebResourceRequest request) {
                Intent intent;
                try {
                    intent = Intent.parseUri(request.getUrl().toString(), Intent.URI_INTENT_SCHEME);
                } catch (URISyntaxException e) {
                    intent = null;
                }

                if (intent == null) {
                    // URLからIntentをつくれなかった
                    return;
                }

                ResolveInfo info = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
                if (info != null) {
                    // intentに対応するパッケージがあったときだけstartActivityをする
                    startActivity(intent);
                }
            }
        });
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4