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.