안드로이드
android WebView에서 target="_blank" 속성을 가진 a 태그에 대해서 처리
앤테바
2021. 1. 19. 22:25
반응형
android에 WebView를 사용하고 있습니다.
WebView에서 보여지는 화면에서 아래와 같은 링크 태그를 선택해도 아무런 동작을 하지 않습니다.
다음 맵 지도를 연결하는 링크입니다. 원래 크롬에서는 새로은 탭 페이지에서 다음 지도가 오픈이 정상적으로 됩니다.
<a target="_blank" href="http://map.daum.net/?q=정부세종청사">정부세종청사</a>
구글링을 통해서 스택오버플로우에서 해결책을 찾았습니다.
Android - Open target _blank links in WebView with external browser
I build a WebView which displays a website. The website contains links without a target="_blank" attribute and some with it. I need to open the links with target defined in the external standard d...
stackoverflow.com
해결책 코드는 아래와 같습니다.
// WebView가 multiple 윈도를 지원하도록 설정
wv.getSettings().setSupportMultipleWindows(true);
wv.setWebChromeClient(new WebChromeClient() {
// host application에게 새로운 윈도우를 생성하라고 요청
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
{
// 사용자가 웹뷰에서 클릭한 정보를 획득
WebView.HitTestResult result = view.getHitTestResult();
// url 획득
String data = result.getExtra();
// url을 open
Context context = view.getContext();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
context.startActivity(browserIntent);
return false;
}
});
반응형