16

My project is mostly a web application. Now I have an android app with a home screen widget and would like to open a web page in the built-in browser on some user actions. That is easy. In MyAppWidgetProvider.onUpdate I create a new intent:

Intent intent = new Intent("android.intent.action.VIEW", 
    Uri.parse("http://my.example.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.text, pendingIntent);

Unfortunately, a new browser window/tab is opened every time the user clicks the widget. My page contains some fancy AJAX/javascript running in the background and regularly sending http requests. So I end up having 8 tabs containing the same page and continuously sending 8-fold amount of http requests.

Is there any way, e.g. different action or additional parameters to avoid opening new tabs?

As a workaround I am now thinking about creating a new activity containing a WebView, but would like to avoid this complexity.

geekQ
  • 29,027
  • 11
  • 62
  • 58

2 Answers2

21

Yes, this is possible. Use http://developer.android.com/reference/android/provider/Browser.html#EXTRA_APPLICATION_ID to ensure that the same browser tab is reused for your application. For example:

Context context = widget.getContext();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
Carter Maslan
  • 493
  • 4
  • 11
  • Thanks! EXTRA_APPLICATION_ID has been even available since API level 3 (Android 1.5)! – geekQ Oct 23 '12 at 12:06
  • 1
    `getPackageName()` is partially work only in my case (Tablet Android 4.1.1). It did launch the browser with the same tab, but press BACK, the browser do not go back previous page, just finish it. By [this](http://stackoverflow.com/questions/9902225/browser-extra-application-id-do-not-work-in-the-tablet-do-you-know-other-work-a/9917548#9917548), it works!!!! – Yeung May 20 '14 at 08:50
  • Worked perfectly. I used intent.putExtra(Browser.EXTRA_APPLICATION_ID, getApplicationContext().getPackageName()); – glez Jan 31 '17 at 14:38
  • Doesn't work. It's still opening in original browser with multiple tabs. – 6rchid Sep 08 '22 at 21:41
  • Didn't work. It's still opening in original browser with multiple tabs. But [this](https://stackoverflow.com/a/73655406/4261600) works for me. – 6rchid Sep 08 '22 at 22:03
-1

To the best of my knowledge there is no way to specify if a new tab is opened or not using the Intent. I even did some looking and could not find anyway to check which tabs were open. Another thing you may be able to try is keeping track of if the user has recently been sent to the browser activity from your application, and check if the browser is still open, but once again I can see this going wrong in numerous ways.

Your activity idea would probably be one of the best bets in this situation, though I can understand why you would want to avoid that.

Ross Hays
  • 341
  • 4
  • 19
  • "You could always end the process for the browser, and that should close all tabs, but I doubt that is the ideal solution." -- that's not possible, except maybe if his app is running as root on a rooted phone. – CommonsWare Mar 30 '11 at 18:43
  • Not possible? It is definitely possible to end a task, how else would there be so many task killers? Now I just tried it though and it opened the same tabs when I restarted the browser, so _that_ part may not work, but you can definitely end tasks. – Ross Hays Mar 30 '11 at 18:48
  • Sorry, task killers were de-fanged after Android 2.1. A process cannot kill another process on newer versions of Android. – CommonsWare Mar 30 '11 at 19:04
  • Worked fine on 2.3 just now...? Idk maybe I am missing something. Doesn't really matter, either way the browser came back on with the same tabs so it wouldn't work for him. – Ross Hays Mar 30 '11 at 19:06
  • You could specify the Intent.FLAG_ACTIVITY_CLEAR_TASK flag to your intent to open a browser. This will clear any currently open tabs. – Matthew Jan 10 '12 at 22:50
  • @Matthew: I tested this on 4.0.3 emulator, it seems to work sometimes and sometimes it does not. – Viktor Brešan Jun 20 '12 at 10:23