解决java.lang.IllegalStateException: Calling View methods on another thread than the UI thread

来源:互联网 发布:上海夏易网络怎么样 编辑:程序博客网 时间:2024/05/17 06:45

在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 >=19return WebSettings.getDefaultUserAgent(ctx); }


0 0
原创粉丝点击