티스토리 뷰

반응형

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

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

 

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

How can I open a URL in Android's web browser from my application?

내 애플리케이션에서 안드로이드 웹 브라우저의 URL을 열려면 어떻게 해야 하나요?

 문제 내용 

How to open an URL from code in the built-in web browser rather than within my application?

내 응용 프로그램이 아닌 내장 웹 브라우저에서 코드에서 URL을 여는 방법은 무엇입니까?

 

I tried this:

내가 해봤어요

 

try {
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
    startActivity(myIntent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "No application can handle this request."
        + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

 

but I got an Exception:

하지만 Exception이 발생했어요:

 

No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com

 

 

 높은 점수를 받은 Solution 

Try this:

다음을 수행하십시오.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

 

That works fine for me.

그것은 나에게 잘 통한다.

 

As for the missing "http://" I'd just do something like this:

누락된 "http://"에 대해서는 다음과 같이 하겠습니다.
if (!url.startsWith("http://") && !url.startsWith("https://"))
   url = "http://" + url;

 

I would also probably pre-populate your EditText that the user is typing a URL in with "http://".

사용자가 "http://"로 URL을 입력하는 편집 텍스트를 미리 채울 수도 있습니다.

 

 

 

 가장 최근 달린 Solution 

A new and better way to open link from URL in Android 11.

안드로이드 11에서 URL에서 링크를 여는 새롭고 더 나은 방법.

 

  try {
        val intent = Intent(ACTION_VIEW, Uri.parse(url)).apply {
            // The URL should either launch directly in a non-browser app
            // (if it’s the default), or in the disambiguation dialog
            addCategory(CATEGORY_BROWSABLE)
            flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_REQUIRE_NON_BROWSER or
                    FLAG_ACTIVITY_REQUIRE_DEFAULT
        }
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        // Only browser apps are available, or a browser is the default app for this intent
        // This code executes in one of the following cases:
        // 1. Only browser apps can handle the intent.
        // 2. The user has set a browser app as the default app.
        // 3. The user hasn't set any app as the default for handling this URL.
        openInCustomTabs(url)
    }

 

References:

참조:

https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9 and https://developer.android.com/training/package-visibility/use-cases#avoid-a-disambiguation-dialog

 

 

출처 : https://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application

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