在UI线程的WebView Twitter的登录窗口 [英] WebView Twitter login window on UI thread

查看:136
本文介绍了在UI线程的WebView Twitter的登录窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图允许用户在通过一个的WebView 登录本对话preference 我的应用这项工作很好,但我似乎停留在如何做到这一点正确的。

I'm attempting to allow a user to login via a WebView within a DialogPreference my application and this work fine, but I seem to be stuck on how to do this properly.

如果我这样做,因为接下来,我得到一个错误

If I do this as next, I get an error

错误

Caused by: android.os.NetworkOnMainThreadException
 E/AndroidRuntime(732):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)

code

@Override
protected void onBindDialogView(View v) {
    super.onBindDialogView(v);

    this.webViewOauth = (WebView) v.findViewById(R.id.web_oauth);     
    try {
        String authURL = this.backend.getRequestToken().getAuthenticationURL();
        webViewOauth.loadUrl(authURL);
    }
    catch (TwitterException e) {
        e.printStackTrace();
    }       
}

我可以通过改变解决这个问题的 ThreadPolicy ,但据我所知,有这种限制的原因

I can fix this by changing the ThreadPolicy, but as far as I can tell, there is a reason for this restriction

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

所以,我这个切换到的AsyncTask ,我得到一个警告

警告

java.lang.Throwable中:警告:的WebView方法被调用线程上
  AsyncTask的#4。所有的WebView方法必须在UI线程调用。
  的WebView的未来版本可能不支持在其他线程使用。

java.lang.Throwable: Warning: A WebView method was called on thread 'AsyncTask #4'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

code

@Override
protected void onBindDialogView(View v) {
    super.onBindDialogView(v);

    MyLoginTask login = new MyLoginTask(this, v, this.backend);
    login.execute();
}

这两种方法的工作,但有正确做到这一点的具体方法是什么?在 ThreadPolicy 似乎是一个黑客攻击,并可能导致其阻塞UI线程,但I'n不知道它居然会,而的AsyncTask 方法显然是不正确也基于此警告。

Both methods work, but is there a specific way to do this properly? The ThreadPolicy seems to be a hack and could cause it to block the UI thread, but I'n not sure if it actually will, while the AsyncTask method clearly is incorrect too based on the warning.

这是一个合适的时间来设置 ThreadPolicy ,因为它实际上运行的是UI元素?

Is this an appropriate time to set the ThreadPolicy since it actually is running a UI element?

推荐答案

是,使用ScrictMode政策的的一个黑客,以及可怕的一个作为陈述的这里和<一个href=\"http://stackoverflow.com/questions/9413625/android-android-os-networkonmainthreadexception?lq=1\">here

Yes, using the ScrictMode Policy is a hack, and a terrible one as stated here and here

您有两种方法来解决这个问题:

You have two ways to solve this:

1,从您的MyLoginTask的onPostExecute()方法返回 authURL 。简单地定义调用MyLoginTask对象的setter,并有onPostExecute()的结果分配给它。

1- Return the authURL from the onPostExecute() method of your MyLoginTask. Simply define a setter in the object that calls the MyLoginTask, and have onPostExecute() assign the result to it.

2 - 传递给你的web视图的MyLoginTask参考,并加载它的URL在onPostExecute()。由于onPostExecute()将在UI线程始终运行,将摆脱的警告。

2- Pass a reference to your webview to the MyLoginTask, and have it load the url in the onPostExecute(). Because onPostExecute() will always run in the UI thread, you will get rid of the warning.

在任何情况下,请了约'无痛线程Android开发者检查出这个职位;你一定会发现它的帮助: HTTP://android-developers.blogspot。 CA / 2009/05 /无痛-threading.html

In any case, do check out this post by the Android developers about 'Painless Threading'; you will certainly find it helpful: http://android-developers.blogspot.ca/2009/05/painless-threading.html

这篇关于在UI线程的WebView Twitter的登录窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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