如何在安卓应用中使用谷歌浏览器隐身模式打开网页

10

我花了很多时间,仍然无法弄清楚这个问题。

我需要在隐身模式下启动Chrome浏览器。

我的代码:

    private void launchBrowser() {
    String url = "http://foyr.com";
    Intent launchGoogleChrome = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    launchGoogleChrome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    launchGoogleChrome.setPackage("com.android.chrome");
    try {
        startActivity(launchGoogleChrome);
    } catch (ActivityNotFoundException e) {
        launchGoogleChrome.setPackage(null);
        startActivity(launchGoogleChrome);
    }
}

我发现有几篇帖子,但是无法找到解决方案。这里
这个链接给了我一些关于隐身模式的想法,但我也尝试过。
    private void launchBrowser() {
    String url = "http://foyr.com";
    Intent launchGoogleChrome = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    launchGoogleChrome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    launchGoogleChrome.setPackage("com.android.chrome");
    launchGoogleChrome.putExtra("com.android.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", true);
    try {
        startActivity(launchGoogleChrome);
    } catch (ActivityNotFoundException e) {
        launchGoogleChrome.setPackage(null);
        startActivity(launchGoogleChrome);
    }
}

但Chrome浏览器没有从应用程序接收到任何意图信息。有人能帮我看看哪里出了错,并告诉我该怎么做吗?

可能有一种使用EXTRA_OPEN_NEW_INCOGNITO_TAB的解决方案,但我认为从Chrome之外无法实现。 - Mathieu de Brito
有没有可以导航到隐身模式的Android意图? - GvSharma
1
类似的问题自多年前以来就已经被发布,甚至没有一个专家能够回答这些问题。我现在很好奇...也许根本就没有解决方案。 - ganero
1个回答

4

从源代码中:

// "Open new incognito tab" is currently limited to Chrome or first parties.
if (!isInternal
        && IntentUtils.safeGetBooleanExtra(
                   intent, EXTRA_OPEN_NEW_INCOGNITO_TAB, false)) {
    return true;
}

看起来只有在您分叉Chrome或经过Google明确允许时,才会执行额外操作。


问题中的Chromium代码链接:https://source.chromium.org/chromium/chromium/src/+/main:chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java;l=918-931 - Chris Lacy
这是进行更改的Chromium提交链接:https://source.chromium.org/chromium/chromium/src/+/1b2247e8e0a7277aa58ce4fcb242c7be2dafe564 - Chris Lacy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接