1

I have an application which logs in into a webservice. I start a new thread when the login button is clicked and show a progress dialog while the device is communicating with the server. Based on the success or failure of the attempt I send a message to the handler of 1 - success and -1 -failure. But how do I create and start a new intent based on this message? Or how should I handle this situation otherwise. Here is my code for the Login Activity:

public class Login extends Activity implements OnClickListener {

private static final int DIALOG = 0;
private static String TAG="LoginActivity";
private ProgressDialog progressDialog;

private final Handler handler =  new Handler() {
    public void handleMessage(Message msg) {

        int state = msg.arg1;
        if (state != 0)
        {
            Log.i(TAG, "dialog dismissed, message received "+state);
            dismissDialog(DIALOG);
        }


    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    View loginButton = findViewById(R.id.login_button);
    loginButton.setOnClickListener(this);

}

public void onClick(View v) {

    switch (v.getId()) {
    case R.id.login_button:
        Log.i(TAG,"login button");
        showDialog(DIALOG);

        Intent i = new Intent(this, PTVActivity.class);
        startActivity(i);
        break;

    }
}

protected Dialog onCreateDialog(int id)
{
    switch(id) {
    case DIALOG:
        progressDialog = new ProgressDialog(Login.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(getString(R.string.login_progress));
        return progressDialog;
    default:
        return null;
    }
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch(id) {
    case DIALOG:

        EditText nameText=(EditText)findViewById(R.id.login_name);
        EditText pwText=(EditText)findViewById(R.id.password);
        String name = nameText.getText().toString();
        String pw = pwText.getText().toString();

        CommunicateServer communicate= new CommunicateServer(name,pw,handler);
        communicate.run();
    }


}

}

and here is the run() method of my CommunicateServer class:

@Override
public void run() {


    Message msg = handler.obtainMessage();
    if (login()==true)msg.arg1 = 1;
    else msg.arg1 = -1;
    handler.sendMessage(msg);

}
Pio
  • 4,044
  • 11
  • 46
  • 81

1 Answers1

0

To create a new Intent and start an activity you have to do following:

Intent intent = new Intent(Login.this, anotherActivity.class);
startActivity(intent);

The simplest way of handling this type of work is to use AsyncTask (example):

In your case you have to show a Progress dialog in onPreExecute() method of AsyncTask.

In the doInBackground(..) you will be making a server call for authentication. You have to return the status (1 or -1) from doInBackground(..) method.

Finally you will get the status value (1 or -1) in onPostExecute(..) method as a parameter. Here you will be taking decision based on the status. If it is 1 you will be creating a new intent to open an Activity. You should also dismiss the progress dialog in this method.

Community
  • 1
  • 1
rizzz86
  • 3,862
  • 8
  • 35
  • 52
  • But onPostExecute() is in the AsyncTask class. So this does not change anything, its like I interchange the handleMessage() method from my code above to the onPostExecute(). I saw that AsyncTask has a get() method but if I call it my thread will be blocked so my progressbar will be useless. I need a simple interthread communication mechanism where I can send values from one to another – Pio Jun 13 '12 at 22:25
  • You haven't created any Intent inside your handleMessage() method. I mean you have to create intent "Intent intent = new Intent(this, yourActivity.class)" and start it. – rizzz86 Jun 14 '12 at 06:45
  • But what context will it get? Because when creating a handler does not need a context. How do I pass one? It is possible only if I subclass the Handle. which is an ugly solution. – Pio Jun 14 '12 at 07:28
  • 1
    Put Login.this in the context parameter – rizzz86 Jun 14 '12 at 07:37