Android app opening browser in the same tab?

半腔热情 提交于 2020-02-28 23:33:38

问题


I'm finding this hard to explain, but i'll do my best. I have an app that opens the browser (firefox) and send information from that app to the webpage as a php variable, the problem i have is that i am doing this quite a lot, and everytime it opens the browser its opening a new tab.

I know there is no way to close a tab using javascript etc, so is there a way to ensure it always opens into the same current tab so i dont end up with several open at once.

I dont want to keep having to close firefox tabs whenever the app fires up the browser.

Sorry if its hard to make sense of. Thanks.


回答1:


Probably this is a bit late, but I have managed to accomplish what you want only using Chrome app.

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);

This allows you to re-use the current opened tab on Chrome and change the current URL in that tab to the one you are providing in the intent.

I also have tried with no success on Firefox.

I hope this helps.




回答2:


For Firefox android app to replace the existing url of current tab.

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);

For chrome or other browsers, just change the package name. Hopefully it will solve the problem.




回答3:


Answer used to open a "sample.html" file in same tab instead of creating a new tab of Google Chrome browser.

       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());
            }
        }
    });

Note : Kindly check your Storage permission before proceeding.



来源:https://stackoverflow.com/questions/36823802/android-app-opening-browser-in-the-same-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!