运行相同的AsyncTask多次顺序 [英] Running same Asynctask multiple times sequentially

查看:154
本文介绍了运行相同的AsyncTask多次顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按顺序执行相同的AsyncTask。例如,让我们有一个的AsyncTask的 A 。现在假设的 A 检查,如果某些服务器正忙或没有。如果是忙碌的,它再次检查....等等。比方说,它会检查这个5次,然后让用户知道该服务不可用。


所以,同样的AsyncTask显然可以使用。


试图与循环来实现它可能会产生意想不到的结果与根据本
链接
AsyncTask的是一种发射后不管实例的AsyncTask实例只能使用一次。即可。
所以循环5次指的AsyncTask将被调用5次。但是,如果服务器是免费的第2次尝试,我并不需要额外的检查。另外Android应用程序可能会挂起(我做了)。纠正我,如果我错了,就这个话题。

I am trying to execute same Asynctask sequentially. For example, Let there be an Asynctask A. Now assume A checks if some server is busy or not. If it's busy, it checks again.... And so on. Let's say it checks this for 5 times then let users know that the service is unavailable.
So same Asynctask can obviously be used.
Trying to achieve it with loops may yield unexpected result as according to this link,
Asynctask is a fire-and-forget instance and AsyncTask instances can only be used one time..
So looping 5 times means Asynctask will be called 5 times. But if the server is free on the 2nd attempt, I don't need additional checking. Also android application may hang(mine did). Correct me if I'm wrong on this topic.

另外,回答是,调用任务如新MyAsyncTask()执行。();


所以,如果我做这样的 - 例如,

Also the answer was, "calling task like new MyAsyncTask().execute("");"
So if i do like this -- for example,

new someAsyncTask() {
            @Override
            protected void onPostExecute(String msg) {
                new someAsyncTask() {
                    @Override
                    protected void onPostExecute(String msg) {
                      ....and so on 5 times.
                   }
               }.execute(this);
            }
        }.execute(this);

应该依次5次打来电话,应该不会出现问题。

它是这样做的唯一途径或任何其他解决方案是present?

It should be called 5 times sequentially and no problems should occur.
Is it the only way to do this or any other solution is present?

推荐答案

您可以在外部类中使用一个字段变量:

You could use a field variable in the outer class:

private int count;
private void attemptConnect(){
    count = 0;

    new MyTask().execute("");
}

class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        count++;
        if(count < 5){
            new MyTask().execute("");
        }
    }
}

或者传递一个计数的AsyncTask的构造器:

Or pass in a count to the AsyncTask constructor:

private void attemptConnect(){
    new MyTask(0).execute("");
}

class MyTask extends AsyncTask<String, String, String> {
    private int count;

    public MyTask(int count){
        this.count = count;
    }

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        if(count < 4){
            new MyTask(++count).execute("");
        }
    }
}

这篇关于运行相同的AsyncTask多次顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆