在我的应用程序中,我通过 Chrome 自定义选项卡打开了一个网址。我们知道,当用户点击设备后退按钮或自定义后退按钮时,Chrome 自定义选项卡将关闭。是否可以通过编程方式关闭 Chrome 自定义选项卡而无需用户干预。
目前不支持以编程方式关闭 Chrome 自定义选项卡。但如果需要,您可以通过从启动 Chrome 自定义选项卡的位置启动上一个活动来关闭它。
让您从“MainActivity”打开Chrome自定义选项卡,Chrome自定义选项卡中有一个选项菜单项“Close”,在“Close”菜单项上单击您想要关闭的Chrome自定义选项卡并返回“MainActivity”,然后您可以通过启动“MainActivity”来完成此操作。为此,请将您的活动启动模式设置为
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
已安装在设备上。因为在此演示代码中,chrome 自定义选项卡已通过将硬编码包设置为 com.android.chrome
打开,并且该应用程序可能会在未安装 Chrome
的系统上崩溃。不。需要用户同意才能关闭自定义选项卡视图。
android:noHistory =“true”在活动中的manifest.xml中使用此行,它可以完美工作
您可以使用自定义 URI 方案链接回您的应用程序。只需确保使用意图过滤器注册您的方案(请参阅深层链接)。然后就可以开始活动了:
private void closeBrowserTab(Context context, String customUri) {
Uri uri = Uri.parse(customUri);
Intent appIntent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(appIntent);
}
将值保存在首选项或数据存储中并重新启动活动,然后检查保存的值并打开任何内容或执行某些操作
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)
}