简体   繁体   English

如何关闭 Chrome 自定义标签

[英]How to close chrome custom tabs

In my app I have opened a url via Chrome Custom Tab.在我的应用程序中,我通过 Chrome 自定义选项卡打开了一个 url。 We know that when user taps the device back button or custom back button Chrome Custom Tab will be closed.我们知道,当用户点击设备后退按钮或自定义后退按钮时,Chrome 自定义选项卡将被关闭。 Is it possible to close the Chrome Custom Tab by programatically without user intervention.是否可以在没有用户干预的情况下以编程方式关闭 Chrome 自定义选项卡。

There is no such support currently to close chrome custom tab programatically.目前没有此类支持以编程方式关闭 chrome 自定义选项卡。 But you can close it by starting your previous activity from where you launched chrome custom tab if you want.但是,如果需要,您可以通过从启动 chrome 自定义选项卡的位置开始上一个活动来关闭它。

Let, you open chrome custom tab from " MainActivity " and there is a option menu item " Close " in chrome custom tab, and on " Close " menu item click you want to close chrome custom tab and to go back the " MainActivity ", then you can do it by starting " MainActivity ".让您从“ MainActivity ”打开chrome自定义选项卡,在chrome自定义选项卡中有一个选项菜单项“ Close ”,然后在“ Close ”菜单项上单击要关闭chrome自定义选项卡并返回“ MainActivity ”,然后你可以通过启动“ MainActivity ”来做到这一点。 For this, set your activity launchMode as singleTask and then start your activity with FLAG_ACTIVITY_CLEAR_TOP when button is clicked.为此,设置你的活动launchMode为singleTask ,然后用启动活动FLAG_ACTIVITY_CLEAR_TOP按钮被点击时。

Check my demo code for details, hope it will help someone who want something like this.查看我的演示代码以获取详细信息,希望它能帮助那些想要这样的人。

AndroidManifest.xml : 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 :主活动.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 : 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:活动_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" />

Note:笔记:
Make sure that updated Chrome has installed on device by explicit checking before testing this code.在测试此代码之前,通过显式检查确保更新的Chrome已安装在设备上。 Because in this demo code chrome custom tab has opened by setting a hard-coded package to com.android.chrome and app may break on systems that don't have Chrome installed.因为在此演示代码中,通过将硬编码包设置为com.android.chrome来打开 chrome 自定义选项卡,并且应用程序可能会在未安装Chrome系统上中断。
So please follow the Best Practices to launch chrome custom tab.因此,请按照最佳实践启动 Chrome 自定义选项卡。

否。需要用户同意才能关闭自定义选项卡视图。

android:noHistory="true" 在活动的 manifest.xml 中使用这一行它是完美的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM