Search code examples
androidmultithreadingkeyeventonkeydown

What to replace the KeyEvent?


Basically, I want to have the spinning wheel load up immediately when the activity starts up, and then disappear when it is finished. However, in my current code, it only starts when I press a key. How should I replace the onKeyDown method so that everything starts up automatically?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    pd = ProgressDialog.show(this,"Working", "Fetching Grades", true, 
            false);
    Thread thread = new Thread(this);
    thread.start();
    return super.onKeyDown(keyCode, event);
}
public void run(){
    //String userthat = userThing.getText().toString();
    //String passthis = passThing.getText().toString();
    thisthing = new ClientLoginForm(user2s,pas2s);
    try{
        grades +=thisthing.returnGradies();
    } catch (Exception e){
        grades +="Connection Problems";
    }
    handler.sendEmptyMessage(0);
}
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        pd.dismiss();
        tv.setText(grades + "\n " + pas2s + " " + user2s);
    }
};

Solution

  • put onkeyDown() code into activities onResume() It will help you to solve problem.