Android应用在同一标签中打开浏览器?

时间:2016-04-24 13:31:26

标签: android firefox android-browser

我发现这很难解释,但我会尽我所能。 我有一个应用程序打开浏览器(firefox)并将该应用程序中的信息作为php变量发送到网页,我的问题是我这样做了很多,并且每次打开浏览器时都会打开一个新的选项卡

我知道没有办法使用javascript等关闭标签,所以有没有办法确保它始终打开到同一个当前标签,所以我不会一次打开几个。

我不想在应用程序启动浏览器时不得不关闭firefox标签。

对不起,如果很难理解。 感谢。

3 个答案:

答案 0 :(得分:1)

可能这有点晚了,但我已经成功完成了你想要的只使用Chrome应用程序。

String url = "http://www.mypage.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.chrome");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

这允许您重新使用Chrome上当前打开的标签,并将该标签中的当前网址更改为您在意图中提供的网址。

我也尝试过在Firefox上没有成功。

我希望这会有所帮助。

答案 1 :(得分:0)

供Firefox android应用替换当前标签页的现有网址。

String package = "org.mozilla.firefox";
String url = "http://www.test.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage(package);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, package);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

对于chrome或其他浏览器,只需更改软件包名称即可。希望它能解决问题。

答案 2 :(得分:0)

用于在同一标签中打开“ sample.html”文件而不是在Google Chrome浏览器中创建新标签的答案。

       btnClickMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            File file = new File(Environment.getExternalStorageDirectory(), "sample.html");
            Uri uri2 = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri2);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.Main");
            intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.chrome");
            try {
                startActivity(intent);
            } catch (Exception e) {
                Log.e("Exception ", e.getLocalizedMessage());
            }
        }
    });

注意:请在继续操作之前检查您的存储权限。