Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to a Socket locks up UI

I'm building (well, trying to build) a simple usenet news reader. The code below works. Is grabs the username, host, password from the SharedPreferences and connects to the server and sucessfully authenticates, however it locks up the UI until all the tasks are done.

How would i change this code so that it doesn't lock up the 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 );

}


}
like image 835
dotty Avatar asked Jul 29 '11 11:07

dotty


2 Answers

Check out AsyncTask

like image 101
Paweł Obrok Avatar answered Sep 27 '22 21:09

Paweł Obrok


I'm sure some of the Android experts will point you in the direction of the various frameworks you'll want to use to implement this but the basic problem is as follows.

The User Interface is single threaded, that thread is often referred to as the Event Dispatch Thread. So when a user clicks on a button and you do something that takes a long time, that stops the UI from doing anything else at the same time.

What you need to do is to do any long running task in a different thread and ensure the communication between the EDT thread and your worker thread is thread safe.

like image 43
brain Avatar answered Sep 27 '22 20:09

brain