如何关闭Chrome自定义标签 [英] How to close chrome custom tabs
问题描述
在我的应用中,我已通过Chrome自定义标签打开了一个网址.我们知道,当用户点击设备后退按钮或自定义后退按钮时,Chrome自定义标签将关闭.是否可以通过编程方式在无需用户干预的情况下关闭Chrome自定义标签.
In my app I have opened a url via Chrome Custom Tab. We know that when user taps the device back button or custom back button Chrome Custom Tab will be closed. Is it possible to close the Chrome Custom Tab by programatically without user intervention.
推荐答案
当前尚无此类支持以编程方式关闭chrome自定义标签.但是,您可以根据需要从启动chrome自定义标签的位置开始上一个活动来关闭它.
There is no such support currently to close chrome custom tab programatically. But you can close it by starting your previous activity from where you launched chrome custom tab if you want.
让我们从" MainActivity "打开chrome定制标签,然后在chrome定制标签中有一个选项菜单项"关闭",在"关闭"中"菜单项,然后单击要关闭Chrome自定义标签并返回" MainActivity ",然后可以通过启动" MainActivity "来实现.为此,将活动启动模式设置为 singleTask
,然后在单击按钮时从 FLAG_ACTIVITY_CLEAR_TOP
开始活动.
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". For this, set your activity launchMode as singleTask
and then start your activity with FLAG_ACTIVITY_CLEAR_TOP
when button is clicked.
查看我的演示代码以获取详细信息,希望它能对想要这样的人有所帮助.
Check my demo code for details, hope it will help someone who want something like this.
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自定义标签.
Note:
Make sure that updated Chrome
has installed on device by explicit checking before testing this code. 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.
So please follow the Best Practices to launch chrome custom tab.
这篇关于如何关闭Chrome自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!