【问题标题】:Connect to a Socket locks up UI连接到 Socket 会锁定 UI
【发布时间】:2011-07-29 11:24:07
【问题描述】:

我正在构建(嗯,尝试构建)一个简单的 usenet 新闻阅读器。下面的代码有效。 Is 从 SharedPreferences 中获取用户名、主机、密码并连接到服务器并成功进行身份验证,但它会锁定 UI,直到所有任务完成。

我将如何更改此代码使其不会锁定 UI?

package com.webfoo.newz;

import java.io.IOException;
import java.net.SocketException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.apache.commons.net.nntp.NNTPClient;

public class NewzActivity extends Activity {

TextView statusText;
String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings;
NNTPClient nntpClient;
int port;
String username;
String password;
String host;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.statusText = (TextView)findViewById(R.id.connectionStatusTextView);
    this.nntpClient = new NNTPClient();
    this.settings = getSharedPreferences(PREFS_NAME, 0);
}

public void openSettings(View button){
    Intent settingsIntent = new Intent( NewzActivity.this, SettingsActivity.class );
    startActivity( settingsIntent );
}

public void makeConnection(View button) {

    this.statusText.setText("Connecting...");       
    this.port = settings.getInt("UsenetPort", 563);
    this.host = settings.getString("UsenetHost", "");
    this.nntpClient.setDefaultPort( port );
    this.nntpClient.setDefaultTimeout( 9999 );
    // this.nntpClient.setConnectTimeout( 9999 );
    this.statusText.setText("Connecting to " + host );

    try {
        this.nntpClient.connect( host );
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    this.statusText.setText("Connected to " + host );

    if( nntpClient.isConnected() ){
        setAuthDetails();
    }else{
        this.statusText.setText("Failed to Connected to " + host );
    }

}

private void setAuthDetails() {

    this.username = settings.getString("UsenetUsername", "");
    this.password = settings.getString("UsenetPassword", "");

    try {
        nntpClient.authinfoUser(username);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        nntpClient.authinfoPass(password);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    statusText.setText("Authenticated as " + username );

}


}

【问题讨论】:

    标签: java android sockets


    【解决方案1】:

    查看AsyncTask

    【讨论】:

    • 我可以将我的 nntpClient 对象传递给 AsyncTask(以及用户名、密码、主机字符串和其他所有内容吗?)
    • 您创建了一个 AsyncTask 的子类,是的,您可以将参数传递给它,或者创建一个带参数的构造函数。
    • 你能举一个传递这些变量的例子吗?
    【解决方案2】:

    我相信一些 Android 专家会为您指明您想要使用的各种框架的方向,但基本问题如下。

    用户界面是单线程的,该线程通常被称为事件调度线程。因此,当用户单击按钮并且您执行需要很长时间的操作时,UI 将无法同时执行其他操作。

    您需要做的是在不同的线程中执行任何长时间运行的任务,并确保 EDT 线程和您的工作线程之间的通信是线程安全的。

    【讨论】:

      【解决方案3】:
      Thread T = new Thread(new Runnable(){
          public void run(){
              ///////////////////////////////
              //YOUR CODE
              ///////////////////////////////
          }
      });
      
      
              //IF YOU WANT TO MANIPULATE THE UI inside the run()
              //USE:
      runOnUiThread(new Runnable() {
      @Override
      public void run() {
          ///////////////////////////////
          //Your Code
          ///////////////////////////////
          }
      });
      

      【讨论】:

      • 最好使用AsyncTask
      猜你喜欢
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多