티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Is `shouldOverrideUrlLoading` really deprecated? What can I use instead?

shouldOverrideUrlLoading
가 정말로 더 이상 사용되지 않는가요? 그렇다면 대신에 무엇을 사용해야 할까요?

 문제 내용 

Is "shouldOverrideUrlLoading" really deprecated? If so, what can I use instead?

shouldOverrideUrlLoading 가 정말로 더 이상 사용되지 않는가요? 그렇다면 대신에 무엇을 사용해야 할까요?

 

It seems like shouldOverrideUrlLoading is deprecated targeting Android N and I need to make an app work since API 19 until the latest right now which is Android N (beta), I use some features that are new in Android N (like Data Saver), so targeting Marshmallow will not help with the issue since I need to use those new features, here is the part of the code I use:

shouldOverrideUrlLoading 이 안드로이드 N을 대상으로 사용되지 않게 되었다는 것 같습니다. 하지만 저는 API 19부터 최신 Android N(beta)까지 모든 버전에서 앱을 작동시켜야 합니다. 제가 사용하는 기능 중 일부는 Android N에서 새롭게 추가된 Data Saver와 같은 새로운 기능입니다. 따라서 마시멜로를 대상으로 하면 새로운 기능을 사용할 수 없으므로 문제를 해결할 수 없습니다. 이것이 제가 사용하는 코드 일부입니다:
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
    if (url.startsWith("http:") || url.startsWith("https:")) {
        ...
    } else if (url.startsWith("sms:")) {
        ...
    }
    ...
}

 

And this is the message Android Studio gave me:

그리고 안드로이드 스튜디오에서 제공한 메시지는 다음과 같습니다:

Overrides deprecated method in 'android.webkit.WebViewClient' This inspection reports where deprecated code is used in the specified inspection scope.

 

Google says nothing about that deprecation.

Google은 이 deprecation에 대해 아무 말도 하지 않았습니다.

 

I wonder if using @SuppressWarnings("deprecation") will let me work on all devices since the API 19 until the latest Android N Beta (and its final version when it gets released), I can't test it myself, I never used that and I need to be sure that it works, so, anyone can tell?

@SuppressWarnings("deprecation")를 사용하면 API 19부터 최신 Android N 베타 (및 공식 버전)까지 모든 장치에서 작동할 수 있는지 궁금합니다. 이것을 직접 테스트할 수 없기 때문에 사용할 수 있는지 확실하게 알아내야 합니다. 도움을 주실 분 계신가요?

 

 

 

 높은 점수를 받은 Solution 

Documenting in detail for future readers:

나중에 읽을 독자들을 위해 자세히 설명하자면:

 

The short answer is you need to override both the methods. The shouldOverrideUrlLoading(WebView view, String url) method is deprecated in API 24 and the shouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is added in API 24. If you are targeting older versions of android, you need the former method, and if you are targeting 24 (or later, if someone is reading this in distant future) it's advisable to override the latter method as well.

단답은 두 가지 메소드를 모두 오버라이드해야 한다는 것입니다. shouldOverrideUrlLoading(WebView view, String url)
메소드는 API 24에서 더 이상 사용되지 않으며 shouldOverrideUrlLoading(WebView view, WebResourceRequest request) 메소드가 API 24에서 추가되었습니다. 이전 버전의 Android를 대상으로 작업하는 경우 전자 메소드를 사용해야 하고, 24를 대상으로 하는 경우 (원격에서 이 글을 읽는 미래의 사람들을 위해서) 후자 메소드를 오버라이드하는 것이 좋습니다.

 

The below is the skeleton on how you would accomplish this:

다음은 이를 구현하는 방법에 대한 기본 구조입니다:
class CustomWebViewClient extends WebViewClient {

    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        final Uri uri = Uri.parse(url);
        return handleUri(uri);
    }

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        final Uri uri = request.getUrl();
        return handleUri(uri);
    }

    private boolean handleUri(final Uri uri) {
        Log.i(TAG, "Uri =" + uri);
        final String host = uri.getHost();
        final String scheme = uri.getScheme();
        // Based on some condition you need to determine if you are going to load the url 
        // in your web view itself or in a browser. 
        // You can use `host` or `scheme` or any part of the `uri` to decide.
        if (/* any condition */) {
            // Returning false means that you are going to load this url in the webView itself
            return false;
        } else {
            // Returning true means that you need to handle what to do with the url
            // e.g. open web page in a Browser
            final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            return true;
        }
    }
}

 

Just like shouldOverrideUrlLoading, you can come up with a similar approach for shouldInterceptRequest method.

shouldOverrideUrlLoading 처럼 shouldInterceptRequest 메소드에 대해서도 비슷한 접근 방식을 사용할 수 있습니다.

 

 

 

출처 : https://stackoverflow.com/questions/36484074/is-shouldoverrideurlloading-really-deprecated-what-can-i-use-instead

반응형
댓글
공지사항
최근에 올라온 글