Open an url in android browser, avoid multiple tabs
My project is mostly a web application. Now I have an android app with a home 开发者_StackOverflow社区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.
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());
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.
精彩评论