Bassios
Bassios

Reputation: 43

Android - Action on progress dialog exit

I'm trying to write a pretty basic app for my phone, just to get myself back into the swing of programming. I've got the framework and the basics of what I want to do, I just need help on a small section that's killing me.

On my app, I have a button. When that button is pressed I want the app to change a piece of text to "Deciding...", pop up an indeterminate progress bar for a second or two then exit, then print the result of another activity, but only after the dialog has exited. I'm using a thread at the moment, but I can't get it to function correctly.

Does anyone have any idea how to get it to work the way I want it to?

Here's my Activity file source:

package com.bassios.decisionengine;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class DecisionEngineActivity extends Activity {

private ProgressDialog progressDialog;
public String decision;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}

public void decide(View view) {
    TextView tv = (TextView) findViewById(R.id.Decision1);
    tv.setText("Deciding...");
    int a = generate();
    decision = decision(a);
    runDialog(1);
//        alterText();
}

public void alterText() {
    TextView tv = (TextView) findViewById(R.id.Decision1);
    tv.setText(decision);
}

private void runDialog(final int seconds) {
    progressDialog = ProgressDialog.show(this, "Please wait...", "Deciding...");
    new Thread(new Runnable() {

        public void run() {
            try {
                Thread.sleep(seconds * 1000);
                progressDialog.dismiss();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public static int generate() {
    int a = (int) (Math.random() * 10) + 1;
    return a;
}

public static String decision(int a) {
    String decision = null;
    switch (a) {
        case 1:
            decision = "Yes";
            break;
        case 2:
            decision = "No";
            break;
        case 3:
            decision = "Yes";
            break;
        case 4:
            decision = "No";
            break;
        case 5:
            decision = "Yes";
            break;
        case 6:
            decision = "No";
            break;
        case 7:
            decision = "Yes";
            break;
        case 8:
            decision = "No";
            break;
        case 9:
            decision = "Yes";
            break;
        case 10:
            decision = "No";
            break;
    }
    return decision;
    }
}

Thanks!

Upvotes: 1

Views: 1326

Answers (1)

gosr
gosr

Reputation: 4708

Use AsyncTask.

An example:

http://javatech.org/2011/02/discovering-android-opening-a-progress-dialog-with-asynctask/

Update:

First of all, you must declare the class like this:

private class DecideTask extends AsyncTask<Void, Void, Boolean>
{
...
}

I don't know how showDialog and removeDialog works, but I would do it like this instead:

Show dialog (in onPreExecute):

progressDialog = ProgressDialog.show(this, "Please wait...",
                "Deciding...", true);

Remove/dismiss dialog (in onPostExecute):

progressDialog.dismiss();

Declaring each of the three methods:

protected Boolean doInBackground(Void... params)

protected void onPreExecute()

protected void onPostExecute(Boolean result)

That should do it.

Upvotes: 1

Related Questions