Programing

Android에서 강제로 링크를 열어 Chrome에서 열 수있는 방법이 있습니까?

lottogame 2020. 12. 15. 08:14
반응형

Android에서 강제로 링크를 열어 Chrome에서 열 수있는 방법이 있습니까?


저는 현재 많은 jQuery 애니메이션으로 개발 된 웹앱을 테스트하고 있으며, 내장 웹 브라우저의 성능이 정말 좋지 않은 것으로 나타났습니다. Chrome에서 테스트하는 동안 웹앱의 성능이 믿을 수 없을 정도로 빠릅니다. iOS에서와 마찬가지로 Android 용 Chrome에서 링크를 강제로 여는 스크립트 유형이 있는지 궁금합니다.


이를 달성하는 더 우아한 방법은 인 Intent.ACTION_VIEW텐트를 정상적으로 사용 하지만 인 com.android.chrome텐트에 패키지 추가하는 것 입니다. 이는 Chrome이 기본 브라우저인지 여부에 관계없이 작동하며 사용자가 선택기 목록에서 Chrome을 선택한 것과 동일한 동작을 보장합니다.

String urlString = "http://mysuperwebsite";
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
try {
    context.startActivity(intent);
} catch (ActivityNotFoundException ex) {
    // Chrome browser presumably not installed so allow user to choose instead
    intent.setPackage(null);
    context.startActivity(intent);
}

최신 정보

Kindle 장치의 경우 :

Amazon Kindle에 Chrome 앱이 설치되어 있지 않은 경우 Amazon Default Browser를 열고 싶은 경우

String urlString = "http://mysuperwebsite";
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
try {
    context.startActivity(intent);
} catch (ActivityNotFoundException ex) {
    // Chrome browser presumably not installed and open Kindle Browser
    intent.setPackage("com.amazon.cloud9");
    context.startActivity(intent);
}

두 가지 해결책이 있습니다.

패키지 별

    String url = "http://www.example.com";
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setPackage("com.android.chrome");
    try {
        startActivity(i);
    } catch (ActivityNotFoundException e) {
        // Chrome is probably not installed
        // Try with the default browser
        i.setPackage(null);
        startActivity(i);
    }

계획에 따라

    String url = "http://www.example.com";
    try {
        Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
        Intent i = new Intent(Intent.ACTION_VIEW, uri);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    } catch (ActivityNotFoundException e) {
        // Chrome is probably not installed
    }

경고! 다음 기술 최신 버전의 Android에서 작동하지 않습니다 . 이 솔루션은 한동안 사용되어 왔기 때문에 참조 용입니다.

    String url = "http://www.example.com";
    try {
        Intent i = new Intent("android.intent.action.MAIN");
        i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
        i.addCategory("android.intent.category.LAUNCHER");
        i.setData(Uri.parse(url));
        startActivity(i);
    }
    catch(ActivityNotFoundException e) {
        // Chrome is probably not installed
    }

제안 된 모든 솔루션이 더 이상 작동하지 않습니다. @pixelbandito 덕분에 그는 저를 올바른 방향으로 안내했습니다. 크롬 소스에서 다음 상수를 찾았습니다.

public static final String GOOGLECHROME_NAVIGATE_PREFIX = "googlechrome://navigate?url=";

그리고 다음 사용법 :

 Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("googlechrome://navigate?url=chrome-native://newtab/"));

그래서 해결책은 (URL이 인코딩되어서는 안됨)

void openUrlInChrome(String url) {
    try {
        try {
            Uri uri = Uri.parse("googlechrome://navigate?url="+ url);
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (ActivityNotFoundException e) {
            Uri uri = Uri.parse(url);
            // Chrome is probably not installed
            // OR not selected as default browser OR if no Browser is selected as default browser
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        }
    } catch (Exception ex) {
        Timber.e(ex, null);
    }
}

이것은 FirefoxOpera 에서 작동합니다.

document.location = 'googlechrome://navigate?url=www.example.com/';


FOllowing up on @philippe_b's answer, I would like to add that this code will not work if Chrome is not installed. There is one more case in which it will not work - that is the case when Chrome is NOT selected as the default browser (but is installed) OR even if no browser is selected as the default.

In such cases, add the following catch part of the code also.

try {
    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
   i.addCategory("android.intent.category.LAUNCHER");
    i.setData(Uri.parse("http://mysuperwebsite"));
    startActivity(i);
}
catch(ActivityNotFoundException e) {
// Chrome is probably not installed 
// OR not selected as default browser OR if no Browser is selected as default browser
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("somesite.com"));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
}


Here's the closest I've found: Writing a url and replacing http with googlechrome will open Chrome, but it doesn't seem to open the url specified. I'm working with a Samsung Galaxy S5, Android 5.0

That's the best I've found - every other solution I've seen on SO has required an Android app, not a webapp.


The different answers above are good but none is complete. This in all suited me the best which will :

try to open chrome web browser and in case exception occurs(chrome is not default or not installed), will ask for choosing the browser from user:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intent.setPackage("com.android.chrome");
                try {
                    Log.d(TAG, "onClick: inTryBrowser");
                    startActivity(intent);
                } catch (ActivityNotFoundException ex) {
                    Log.e(TAG, "onClick: in inCatchBrowser", ex );
                    intent.setPackage(null);
                    startActivity(intent.createChooser(intent, "Select Browser"));
                }

In google play there is a big variety of chrome browser apps with different features

So it's correct to check all of them

fun Context.openChrome(url: String, onError: (() -> Unit)? = null) {
    openBrowser("com.android.chrome", url) {
        openBrowser("com.android.beta", url) {
            openBrowser("com.android.dev", url) {
                openBrowser("com.android.canary", url) {
                    onError?.invoke() ?: openBrowser(null, url)
                }
            }
        }
    }
}

fun Context.openBrowser(packageName: String?, url: String, onError: (() -> Unit)? = null) {
    try {
        startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
            setPackage(packageName)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        })
    } catch (e: ActivityNotFoundException) {
        onError?.invoke()
    }
}

ReferenceURL : https://stackoverflow.com/questions/12013416/is-there-any-way-in-android-to-force-open-a-link-to-open-in-chrome

반응형