Fire event occurred on AsyncTask

My application has login activity and verifies credentials through the website. For this, I (should) use the AsyncTask class. When the login is successful, the global variable is set to true.

Since this is asynchronous, the activity will not wait for the result, so I would like to add an event that is fired by the post-execute method of the AsyncTask class. Then a listenerin the input activity will close and the main action will be visible.

I would like to know if and how it is possible. I tried some examples from other posts, but can't figure it out.

I think I need to do the following: - create interfacein a class AsyncTask - post_executeI raise an event from a method of this class, but how? - put a listenerin input activity, but how?

any help is appreciated.

Regards, Eric

+5
source share
2 answers

I would like to add an event that is fired by the post-execute method of the AsyncTask class. Then the input activity listener closes and the main action will be visible.

onPostExecute()is already a callback, you can create another callback as described, but this is optional. Just pass the link of your login activity to your AsyncTask, then use it to call finish()and startActivity()to onPostExecute().

+5
source

post, Asynctask IntentService, .

public class PostIntentService extends IntentService implements PostTask.Observer {
    private int counter = 0;
    private int retry = 2;
    private Data mData;

    public PostIntentService() {
        super("PostIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        mData = (Data) intent.getSerializableExtra("data");
        // send updating status
        Intent i = new Intent();
        i.setAction(PostResponseReceiver.ACTION_RESPONSE);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        i.putExtra("status", "updating");
        sendBroadcast(i);
        execute();
        counter++;
    }

    @Override
    public void onSuccessPost(String result) {
        // send success status
        Intent i = new Intent();
        i.setAction(PostResponseReceiver.ACTION_RESPONSE);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        i.putExtra("status", "success");
        sendBroadcast(i);
    }

    @Override
    public void onFailedPost(String result) {
        if (counter < retry) {
            execute();
            counter++;
        }
        else {
            // send failed status
            Intent i = new Intent();
            i.setAction(PostResponseReceiver.ACTION_RESPONSE);
            i.addCategory(Intent.CATEGORY_DEFAULT);
            i.putExtra("status", "failed");
            i.putExtra("data", mData);// for reproduct
            sendBroadcast(i);
        }
    }
    private void execute() {
        PostTask task = new PostTask(this);
        task.execute();
    }
}

-, ( BroadcastReceiver), , .

public class PostBroadcastReceiver extends BroadcastReceiver {
    public static final String ACTION_RESPONSE = "com.example.android.intent.action.POST_PROCESSED";
    private static final int POST_REQUEST = 100;
    private Observer mObserver;

    public PostBroadcastReceiver(Observer observer) {
        mObserver = observer;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getStringExtra("status").equals("updating")) {
        }
        else if (intent.getStringExtra("status").equals("success")) {
            if (mObserver != null) {
                mObserver.onPostFinished();
            }
        }
        else if (intent.getStringExtra("status").equals("failed")) {
            if (mObserver != null) {
                mObserver.onPostFailed();
            }
        }
    }

    public interface Observer {
        public void onPostFinished();
        public void onPostFailed();
    }
}

.

<service android:name=".PostIntentService" />

onCreate .

IntentFilter filter = new IntentFilter(PostBroadcastReceiver.ACTION_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new PostResponseReceiver(this);
registerReceiver(receiver, filter);

.

public void onPostFinished() {
    Log.d("onPostFinished", "onPostFinished");
}
public void onPostFailed() {
    Log.d("onPostFailed", "onPostFailed");
}

onDestroy .

unregisterReceiver(receiver);

, .

Intent intent = new Intent(this, PostIntentService.class);
intent.putExtra("data", mData);
startService(intent);
+2

All Articles