Question

So, I have a login screen. Upon pressing the 'Login' Button a JDBC Connection is made to check the username and password and then move onto the next Activity if the details are correct. As a result of this, the UI hangs for approximately 5 second. I assumed that this was because the connection was created in the same Thread, so I created a new one. I then created a Handler to interact with the UI depending on what happened with this connection.

The trouble is, the UI still hangs. Below is where the new Runnable is declared in the Activity (h is the custom Handler reference belonging to this Activity);

    logInButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                progress.setVisibility(ProgressBar.VISIBLE);
                new LoginProcessor(h).run(); // HERE!
                }});            

Below is the run() method from the LoginProcessor Runnable which includes the code that is causing the hang. The MicroManager class contains simple JDBC database interactions and makes the connection (nothing exciting in there really and I am trying to keep this as short as possible);

public void run() {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
    try{                   
        MicroManager manager = new MicroManager(); // THIS LINE, AND THE LINE BELOW, ARE CAUSING THE HANG!!!!
        if(manager.getEmployeeId(h.getLoginName(), h.getPassword())!= 0){
            h.sendEmptyMessage(0);
        }
    }catch(Exception ex){
        ex.printStackTrace();
        h.sendEmptyMessage(1);
        }        
}

In the above, there is no direct interaction with the UI. Information is simply sent to the Handler so that it can do it on the UI thread. Lastly, here are the methods of my custom Handler called LogInHandler;

@Override
public void handleMessage(Message msg){
    if(msg.what == 0){
        activity.startActivity(new Intent(activity, AdvisorsPanelActivity.class));
        activity.finish();
    }else{
        AlertDialog alertDialog = new AlertDialog.Builder(activity).create();               
        alertDialog.setTitle("Not Working");
        alertDialog.show();
        activity.setProgressVisible(ProgressBar.GONE);
    }
}

public String getLoginName(){
    return activity.getLoginName();
}

public String getPassword(){
    return activity.getPassword();
}   

Sorry to dump so much code on your guys, but I didn't think a complete picture was possible without all the above. I've trimmed it down as much as I can. I've only recently started working with Threading AND Android, so please be gentle with me.

Était-ce utile?

La solution

Based on my experience: Use AsyncTask for JDBC and you shall torture no more.

EDIT :

This is a neat example of implementing AsyncTask:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class AsyncTaskActivity extends Activity implements OnClickListener {

        Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button1);
        // because we implement OnClickListener we only have to pass "this"
        // (much easier)
        btn.setOnClickListener(this);
    }

    public void onClick(View view) {
        // detect the view that was "clicked"
        switch (view.getId()) {
    case R.id.button1:
        new LongOperation().execute("");
        break;
    }
}

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {
        TextView txt = (TextView) findViewById(R.id.output);
        txt.setText("Executed"); // txt.setText(result);
        // might want to change "executed" for the returned string passed
        // into onPostExecute() but that is upto you
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
    }
}

You may want to create and handle your JDBC connection in doInBackground(String... params) part of your code.

Good Luck.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top