Android-打开网站后如何关闭应用程序

问题描述 投票:0回答:2

我开发了一个非常简单的应用程序,可以打开我们公司的网站。

我必须以某种方式对其进行编程,以便它询问用户要在哪个浏览器中打开网站,因为WebView缺少某些功能并存在一些错误。

完整代码:

package XXX;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FullscreenActivity extends AppCompatActivity
{
    public void openWebPage(String url)
    {
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, 0);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        openWebPage("https://www.example.com");
        finish();   // I also tried: System.exit(0);
    }
}

用户被问到他喜欢使用哪个浏览器打开网站。之后,浏览器将打开并加载网站。

问题:该应用程序仍处于打开状态,即使此时我需要关闭它。

enter image description here

启动浏览器后如何关闭应用程序?

我尝试过finish()甚至System.exit(0)都失败了。

android
2个回答
1
投票

您可以在开始活动时使用此标志:

https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

这意味着它将被排除在最近记录之外(即,当您呼叫finish()时,它不会像最近打开的一样出现在其他任何地方。

其他问题:

Remove app from recent apps programmatically

Close application and remove from recent apps/


0
投票

您的活动已完成,它仅在最近的应用程序中显示。并不意味着它是开放的。您需要做些什么才能从最近的应用程序中排除您的活动。就像在清单文件中添加标签一样简单。如果您像下面这样设置android:excludeFromRecents="true",它将阻止活动显示在您正在寻找的近期应用列表中:

<activity
    android:name=".Activity"
    android:excludeFromRecents="true" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 
© www.soinside.com 2019 - 2024. All rights reserved.