Android opens browser from service avoiding multiple tabs

I am trying to open a browser window with my service link that opens on the current browser tab. when i use

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); 

If the browser was open and in the foreground, before my service launches the intention and the browser opens the link in the same window / tab. If the browser was minimized and the service launched the intent, the browser will open the web page in a new window / tab.

I canโ€™t use the web view as it should be in the web browser and it will only work with the default browser. I checked the stream on Open url in Android browser, avoid multiple tabs , but it had no answer. I also tried closing the browser, but this also does not work.

Thanks for the help!

+8
java android
source share
2 answers

If you want to use your application to guess this intention, so call your application directly through the package name, check the following code:

 Intent in = getPackageManager().getLaunchIntentForPackage("com.package.myappweb"); in.putExtra("myURL","http://www.google.com"); in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity( in ); 

you can pass the url through the intent and get it in your application:

 String url=getIntent().getStringExtra("myURL"); if(url!=null){ /// launch app and use one tab for showing the webpage }else{ //launch app normally. } 

the code you mentioned is used to use the application as a web application .. that will display a popup to use to pick up any browser to deal with the link .. not what it would be for any application that wants open link

good luck

+1
source share

A way to avoid multiple tabs is as follows:

 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName()); startActivity(intent); 

EXTRA_APPLICATION_ID will contain the identifier of your application, and the browser will process the tab opened by your application, instead of opening a new tab. Thus, all pages opened by your application will have the same tab, and you will not have โ€œtab inflationโ€ in the browser.

+1
source share

All Articles