打开在Android浏览器的URL,避免多个标签 [英] Open an url in android browser, avoid multiple tabs

查看:242
本文介绍了打开在Android浏览器的URL,避免多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目主要是一个Web应用程序。现在我有一个主屏幕小部件的Andr​​oid应用程序,并希望在一些用户的动作内置的浏览器打开一个网页。是容易。在MyAppWidgetProvider.onUpdate我创建了一个新的目标:

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

不幸的是,一个新的浏览器窗口/标签将在每次用户点击控件时打开。我的页面包含了一些奇特的AJAX / JavaScript的在后台运行,并定期发送HTTP请求。所以,我最终有8个选项卡包含相同的页面,不断发送HTTP请求的8倍量。

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?

作为一种解决办法,我现在正在考虑创建一个包含一个的WebView一个新的活动,而是想避免这种复杂性。

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

推荐答案

是的,这是可能的。 使用<一个href="http://developer.android.com/reference/android/provider/Browser.html#EXTRA_APPLICATION_ID">http://developer.android.com/reference/android/provider/Browser.html#EXTRA_APPLICATION_ID以保证同一个浏览器标签重新使用您的应用程序。例如:

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

这篇关于打开在Android浏览器的URL,避免多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆