在android4.4+版本中,webview的使用发生了一些改变:
见文档:http://developer.android.com/intl/zh-cn/guide/webapps/migrating.html
我们的工程中要收集userAgent,在4.4之后就不能在主线程new webview了,因此使用文档中提供的方法:
If you need to retrieve the user agent but don't need to store it for your app or do not want to instantiateWebView
, you should use the static method, getDefaultUserAgent()
. However, if you intend to override the user agent string in your WebView
, you may instead want to use getUserAgentString()
.
代码如下:
public static String getUserAgent(Context ctx) {
//api 19 之前
if(Build.VERSION.SDK_INT < 19){
WebView webview = new WebView(ctx);
WebSettings settings = webview.getSettings();
return settings.getUserAgentString();
}
//api >=19
return WebSettings.getDefaultUserAgent(ctx);
}