如何强制浏览器不再每次打开新页面?

发布于 2024-10-30 22:17:22 字数 520 浏览 0 评论 0原文

我想提前感谢所有花时间阅读我的帖子的人(也许会回答它:p)

我正在开发一个非常简单的 Android 应用程序,其目的是显示一个网站。 当然,我已经阅读了文档,并且我使用以下代码来显示页面:

Uri uriUrl = Uri.parse("http://example.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);

该网站正确显示,我在天堂:) 但.. 如果我通过按“主页”按钮退出应用程序(我知道它并没有真正退出,而是神经元)并再次启动它,网络浏览器将打开一个包含我的页面的新选项卡。我希望浏览器不创建新选项卡,而是保留第一次启动时创建的选项卡。

我不想使用 Webview,因为它意味着更多的代码,而且我想要一个非常简单的应用程序。

我是 Android 开发新手,所以请善待我:D 我对错误表示歉意,我知道我的英语很糟糕。

谢谢你们!

I'd like to thank in advance all the people who will take the time to read my post (and maybe answer it :p)

I'm developping a very simple Android application, which aims at displaying a website.
Of course, I've read the documentation, and I'm using the following code to display the page :

Uri uriUrl = Uri.parse("http://example.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);

The site is correctly displaying, I'm on heaven :)
But..
If i quit the application by pressing the HOME button (I know it doesn't really quit, but nervermind) and launches it again, the web browser opens a new tab containing my page. What I would like is the browser not creating a new tab, but keep the one that was created during first launch.

I'd rather not use Webviews since it implies more code, and I want a very simple application.

I'm new to Android development, so please be kind with me :D
I apologize for the errors, I know my English is bad.

Thank you guys !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2024-11-06 22:17:22

我对 android 开发也相当陌生,但根据我的理解,当你按你现在的方式调用浏览器时,你只是在默认浏览器中打开 URL。使用 WebView 是理想的选择,因为它特定于您的应用程序(隔离,或者至少这是我的印象)。使用网络视图并不是非常困难。下面是一些应该可以帮助您顺利进行的代码:

    public void initWebView() {         

        WebView wview;  

        wview = new WebView(getApplicationContext());

        //Add the WebView to the current window
        ((WindowManager)getApplicationContext().getSystemService("window")).addView(wview, new WindowManager.LayoutParams(300, 300));

        wview.clearHistory();
        wview.clearCache(true);

        wview.getSettings().setJavaScriptEnabled(true);
        wview.getSettings().setPluginsEnabled(true);
        wview.getSettings().setPluginState(WebSettings.PluginState.ON);

        wview.getSettings().setRenderPriority(RenderPriority.HIGH);


        wview.setWebViewClient(new WebViewClient()
        {
            public void onPageFinished(WebView view, String url)
            {                               
                //Callback for when pages are finished loading
                wview.layout(0, 0, (int)DESIRED_WIDTH, (int)DESIRED_HEIGHT);
            }

            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                //Callback for when pages are being loaded

            }
         });

        wview.loadUrl("http://blog.ascensionsystems.ca/");
    } 

I'm fairly new to android dev too but from what I understand when you're calling the browser the way you are, you're just opening the URL in the default browser. Using a WebView is ideal because it is then specific to your application (isolated, or at least this is the impression I am under). Using a webview isn't incredibly difficult. Here's some code that should have you well under-way:

    public void initWebView() {         

        WebView wview;  

        wview = new WebView(getApplicationContext());

        //Add the WebView to the current window
        ((WindowManager)getApplicationContext().getSystemService("window")).addView(wview, new WindowManager.LayoutParams(300, 300));

        wview.clearHistory();
        wview.clearCache(true);

        wview.getSettings().setJavaScriptEnabled(true);
        wview.getSettings().setPluginsEnabled(true);
        wview.getSettings().setPluginState(WebSettings.PluginState.ON);

        wview.getSettings().setRenderPriority(RenderPriority.HIGH);


        wview.setWebViewClient(new WebViewClient()
        {
            public void onPageFinished(WebView view, String url)
            {                               
                //Callback for when pages are finished loading
                wview.layout(0, 0, (int)DESIRED_WIDTH, (int)DESIRED_HEIGHT);
            }

            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                //Callback for when pages are being loaded

            }
         });

        wview.loadUrl("http://blog.ascensionsystems.ca/");
    } 
2024-11-06 22:17:22

我一直在处理 WebView,它们完成了这项工作:) 对于 Android 开发新手来说,设置它们有点棘手,但它们非常可配置,因此可以适应所有情况。

所以我的问题的答案可能是:使用WEBVIEW

你的代码对 Ascension 有很大帮助,它让我不必从头开始,并看到了拥有功能性 WebView 所需的一些“核心”方法。

给您的小提示:您在此处使用的一些方法在文档中被标记为“已弃用”(例如,我正在考虑 setPluginsEnabled() )。

最后但并非最不重要的一点是,对于那些想知道的人:如果您使用 Ascension 示例代码,您会注意到您的页面在每次屏幕旋转时都会重新加载。弄清楚为什么会发生这种情况对我来说有点痛苦,但这很正常,请参阅 http://www.devdiv.com/android/docs/guide/topics/resources/runtime-changes.html

I've been dealing with WebViews, and they do the work :) It's a bit tricky to set them up when new to Android development, but they are very configurable, and so can be adapted to all situations.

So the answer to my question could be : USE WEBVIEW !

Your code was a big help Ascension, it allowed me not to start from scratch and to see some "core" methods required to have a functionnal WebView.

Small note for you : some methods you use here are noted as "Deprecated" on the documentation (I'm thinking of setPluginsEnabled() for instance).

Last but not least, for those who wonder : if you take Ascension sample code, you'll notice that your page is reloaded on every screen rotation. This was a little pain for me to figure out why this happened, but it's normal, see http://www.devdiv.com/android/docs/guide/topics/resources/runtime-changes.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文