본문 바로가기

shouldOverrideUrlLoading(WebView view, String url) deprecated 에 따른 버전별 처리

mobile/android by 낼스 2019. 6. 10.

참고

  1. Android Intents with Chrome
  2. https://stackoverflow.com/questions/33151246/how-to-handle-intent-on-a-webview-url
  3. https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29

shouldOverrideUrlLoading(WebView view, String url) deprecated 에 따른 버전별 처리

  • shouldOverrideUrlLoading

    • return true : url 로드 중단
    • return false : url 로드 계속
  • deprecated :> shouldOverrideUrlLoading(WebView view, String url)

    // 안드로이드 6.0이상
    @RequiresApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        String url = request.getUrl().toString();
        return checkUrl(url);
    }
    
    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return checkUrl(url);
    }
    
    private boolean checkUrl(String url) {
        if(url.startsWith("intent://") && url.contains("scheme=myurl")){
            try {
                Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                if (intent != null) {
                    Intent existPackage = getPackageManager().getLaunchIntentForPackage(intent.getPackage());
                    if (existPackage != null) {
                        startActivity(intent);
                    } else {
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                        marketIntent.setData(Uri.parse("market://details?id=" + intent.getPackage()));
                        startActivity(marketIntent);
                    }
                }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            return true;
        }
        return false; // 기본
    }

'mobile > android' 카테고리의 다른 글

apk decompile (apk to jar)  (0) 2019.07.16
Mobile Web App(Android&IOS) Store & Run  (0) 2019.06.10
백그라운드 제한  (0) 2019.01.31
anroid life cycle  (0) 2015.02.17
android icon 해상도  (0) 2015.01.02

댓글