检索的用户代理编程方式
-
26-09-2019 - |
题
有没有方法检索浏览器的用户代理而不具有 WebView
在活动?
我知道这是有可能得到通过 WebView
:
WebView view = (WebView) findViewById(R.id.someview);
String ua = view.getSettings().getUserAgentString() ;
但在我的情况下,我不需要webview对象,我不想创建它只是用于检索的用户代理串。
解决方案
如果你不 已 一个你可以尝试采取这样的
String ua=new WebView(this).getSettings().getUserAgentString();
编辑
医生对 getUserAgentString()
说的
回WebView的用户代理串.
所以我不认为你可以得到它,除非你宣布一个。一些纠正我,如果我错了
其他提示
有一个更简单的方式如果你是在安卓2.1或以上。授予这不是完全相同的用户代理串webview会返回,但是可能为您服务不够好您的目的。
作为一个额外优势拉从网上查看,可以使用这从任何线(不仅仅是UI螺纹)。
有一个系统的财产称为http。代理,这可以用来检索的用户代理串。
String userAgent = System.getProperty("http.agent");
看看 编程方式获得用户代理串 更多的细节。
我用用 解决方案 拟议的通过DeRagan.但事实证明,建立一个单一的 WebView
实例开始一线"WebViewCoreThread"其中停留在背景中的应用程序,直到终止通过该系统。也许它不会占用太多的资源,但我不喜欢它呢。所以我用稍微不同的方法现在,它试图避免WebViewCoreThread创建:
// You may uncomment next line if using Android Annotations library, otherwise just be sure to run it in on the UI thread
// @UiThread
public static String getDefaultUserAgentString(Context context) {
if (Build.VERSION.SDK_INT >= 17) {
return NewApiWrapper.getDefaultUserAgent(context);
}
try {
Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class);
constructor.setAccessible(true);
try {
WebSettings settings = constructor.newInstance(context, null);
return settings.getUserAgentString();
} finally {
constructor.setAccessible(false);
}
} catch (Exception e) {
return new WebView(context).getSettings().getUserAgentString();
}
}
@TargetApi(17)
static class NewApiWrapper {
static String getDefaultUserAgent(Context context) {
return WebSettings.getDefaultUserAgent(context);
}
}
它创造了 WebSettings
实例直接使用程序包可见的构造及如果那不适用于某些原因(例如由于API变化的未来安卓版本)-默默的回落到"WebView状"的解决方案。
更新
作为指通过 @Skywalker5446, 开始从安卓4.2/API17,有一个公共静态的方法得到默认的用户代理的价值。我已经更新我的代码使用这一方法上的支持的平台。
由于安卓2.1你应该使用 系统。getProperty("http。代理");
你也不需要创建一个WebView第一,这就是优势, 你可以用它内部有一个非uithread.
史蒂夫问候
由于Idolon的回答我的应用程序可以处理此在背景中。
但不知何故您激励4G从AT&T运行2.3.3,去赶上的发言和它可以不再运行的背景线。我的解决方案如下:
public static String getUserAgent(Context context) {
try {
Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class);
constructor.setAccessible(true);
try {
WebSettings settings = constructor.newInstance(context, null);
return settings.getUserAgentString();
} finally {
constructor.setAccessible(false);
}
} catch (Exception e) {
String ua;
if(Thread.currentThread().getName().equalsIgnoreCase("main")){
WebView m_webview = new WebView(context);
ua = m_webview.getSettings().getUserAgentString();
}else{
mContext = context;
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
WebView webview = new WebView(mContext);
mUserAgent = webview.getSettings().getUserAgentString();
}
});
return mUserAgent;
}
return ua;
}
}
(假设你已经mContext和mUserAgent领域)
这是一个更新的解决方案基于先前的答复,工作时,编译为奇巧。现在 WebSettings
类是抽象的和 WebSettingsClassic
类已被删除。
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String getUserAgent(final Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return WebSettings.getDefaultUserAgent(context);
}
else {
try {
final Class<?> webSettingsClassicClass = Class.forName("android.webkit.WebSettingsClassic");
final Constructor<?> constructor = webSettingsClassicClass.getDeclaredConstructor(Context.class, Class.forName("android.webkit.WebViewClassic"));
constructor.setAccessible(true);
final Method method = webSettingsClassicClass.getMethod("getUserAgentString");
return (String) method.invoke(constructor.newInstance(context, null));
}
catch (final Exception e) {
return new WebView(context).getSettings()
.getUserAgentString();
}
}
}