如何关闭Chrome自定义选项卡

13
在我的应用程序中,我通过Chrome自定义标签打开了一个URL。我们知道当用户点击设备的返回按钮或自定义返回按钮时,Chrome自定义标签将被关闭。是否有可能在没有用户干预的情况下以编程方式关闭Chrome自定义标签?

3
CustomTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) 可以解决这个问题。 - Rahul
5个回答

16

目前没有支持以编程方式关闭Chrome自定义选项卡的功能。但是,如果您想要关闭它,可以通过从启动Chrome自定义选项卡的先前活动开始关闭它。

例如,您从“MainActivity”打开Chrome自定义选项卡,并且在Chrome自定义选项卡中有一个“Close”选项菜单项。当您单击“Close”菜单项时,您希望关闭Chrome自定义选项卡并返回“MainActivity”,那么您可以通过启动“MainActivity”来实现。为此,请将您的活动launchMode设置为singleTask,然后在单击按钮时使用FLAG_ACTIVITY_CLEAR_TOP启动您的活动。

请查看我的演示代码以获取详细信息,希望它能帮助需要类似功能的人。

AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<receiver
    android:name=".CustomTabReceiver"
    android:enabled="true" />

        

MainActivity.java:

    public class MainActivity extends Activity {
    public static String CHROME_PACKAGE_NAME = "com.android.chrome";
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
    }

    public void onClick(final View view) {
        switch (view.getId()) {
            case R.id.btnOpenChromeCustomTab:
                launchChromeCustomTab();
                break;
            default:
                return;
        }
    }

    private void launchChromeCustomTab() {
        Uri uri = Uri.parse("http://www.google.com/");
        Intent intent = new Intent(mContext, CustomTabReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);

        CustomTabsIntent.Builder customTabsBuilder = new CustomTabsIntent.Builder();
        customTabsBuilder.addMenuItem("Close", pendingIntent);
        CustomTabsIntent customTabsIntent = customTabsBuilder.build();
        customTabsIntent.intent.setPackage(CHROME_PACKAGE_NAME);
        customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        customTabsIntent.launchUrl(mContext, uri);
    }

}

CustomTabReceiver.java :

public class CustomTabReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Intent myIntent = new Intent(context, MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }

    }

activity_main.xml:

<Button
    android:id="@+id/btnOpenChromeCustomTab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:onClick="onClick"
    android:text="Open Chrome Custom Tab" />

注意:
在测试此代码之前,请确保设备上已经安装了更新的Chrome浏览器。因为在这个演示代码中,通过设置一个硬编码的包名com.android.chrome来打开Chrome自定义标签,如果系统上没有安装Chrome,应用程序可能会出现问题。
所以请遵循最佳实践来启动Chrome自定义标签。


非常感谢,这非常有用。 - Hossein Shahdoost
1
当要返回的活动仍在堆栈上时,请使用FLAG_ACTIVITY_SINGLE_TOP而不是FLAG_ACTIVITY_NEW_TASK。 - Marcel Wolterbeek
这很酷,但是在2019年,当它检测到一个URL时,现在不可能关闭它吗? - Joelio

3
不需要用户同意即可关闭自定义选项卡视图。

1

在manifest.xml文件中的activity标签中加入android:noHistory="true"这一行,可以完美地实现功能。


1

保存数值在偏好设置或数据存储中,并且重新启动活动,然后检查保存的数值并打开任何内容或执行某些操作

重新启动活动的代码:

fun restart() {
    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
    startActivity(intent)
    Runtime.getRuntime().exit(0)
}

0

您可以使用自定义的URI方案链接回您的应用程序。只需确保使用意图过滤器(请参见深度链接)注册您的方案即可。然后,您就可以启动活动:

private void closeBrowserTab(Context context, String customUri) {
    Uri uri = Uri.parse(customUri);
    Intent appIntent = new Intent(Intent.ACTION_VIEW, uri);
    context.startActivity(appIntent);
}

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