2011-07-29 5 views
5

간단한 유즈넷 뉴스 리더를 구축하고 있습니다. 아래 코드가 작동합니다. SharedPreferences에서 사용자 이름, 호스트, 암호를 가져 와서 서버에 연결하고 정상적으로 인증하지만 모든 작업이 완료 될 때까지 UI를 잠급니다.소켓에 연결하면 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); 

} 


} 

답변

5

당신이 원하는 것 밖으로 AsyncTask

+0

내가 당신은 AsyncTask를의 서브 클래스를 생성 AsyncTask를 (뿐만 아니라, 사용자 이름, 암호, 호스트 문자열과 다른 모든 것들?) – dotty

+0

내 nntpClient 개체를 전달할 수 있습니다, 그리고 예, 당신은 그것을에 인수를 전달할 수 있습니다, 또는 인수를 취하는 생성자를 만듭니다. – Kaj

+0

변수를 전달하는 예를 들어 주시겠습니까? – dotty

2

나는 안드로이드 전문가 중 일부는 다양한 프레임 워크의 방향을 가리 킵니다 확신 확인이를 구현하기 위해 사용하지만, 다음과 같은 기본적인 문제입니다.

사용자 인터페이스는 단일 스레드이며 해당 스레드는 종종 이벤트 발송 스레드라고도합니다. 따라서 사용자가 버튼을 클릭하고 오랜 시간이 걸리는 작업을하면 UI가 다른 작업을 동시에 수행하지 못하게됩니다.

다른 스레드에서 장기 실행 작업을 수행하고 EDT 스레드와 작업자 스레드 간의 통신이 스레드로부터 안전한지 확인해야합니다.

0
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 
    /////////////////////////////// 
    } 
}); 
+0

AsyncTask를 사용하는 것이 더 좋습니다 – Kaj

+0

@Kaj 예 (: 감사합니다 –