如何立即停止后台运行过程?

时间:2014-02-11 12:47:21

标签: android

我正在实施网络发现流程,我有一个名为ActivityDiscovery的活动,后台处理AbstractDiscovery。一旦执行了发现,它就会进入后台处理并一直工作直到完成处理。即使我按下后退按钮,它也不会停止处理。如果按下后退按钮,如何实现立即停止后台处理?我的AbstractDiscovery也显示在下方。

final public class ActivityDiscovery extends ActivityNet {
    private AbstractDiscovery mDiscoveryTask = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    }

    private void startDiscovering() {            
        mDiscoveryTask.execute();
    }
}

public abstract class AbstractDiscovery extends AsyncTask<Void, HostBean, Void> {

    //private final String TAG = "AbstractDiscovery";

    protected int hosts_done = 0;
    final protected WeakReference<ActivityDiscovery> mDiscover;

    protected long ip;
    protected long start = 0;
    protected long end = 0;
    protected long size = 0;

    public AbstractDiscovery(ActivityDiscovery discover) {
        mDiscover = new WeakReference<ActivityDiscovery>(discover);
    }

    public void setNetwork(long ip, long start, long end) {
        this.ip = ip;
        this.start = start;
        this.end = end;
    }

    abstract protected Void doInBackground(Void... params);

    @Override
    protected void onPreExecute() {
        size = (int) (end - start + 1);
        if (mDiscover != null) {
            final ActivityDiscovery discover = mDiscover.get();
            if (discover != null) {
                discover.setProgress(0);
            }
        }
    }

    @Override
    protected void onProgressUpdate(HostBean... host) {
        if (mDiscover != null) {
            final ActivityDiscovery discover = mDiscover.get();
            if (discover != null) {
                if (!isCancelled()) {
                    if (host[0] != null) {
                        discover.addHost(host[0]);
                    }
                    if (size > 0) {
                        discover.setProgress((int) (hosts_done * 10000 / size));
                    }
                }

            }
        }
    }

    @Override
    protected void onPostExecute(Void unused) {
        if (mDiscover != null) {
            final ActivityDiscovery discover = mDiscover.get();
            if (discover != null) {
                if (discover.prefs.getBoolean(Prefs.KEY_VIBRATE_FINISH,
                        Prefs.DEFAULT_VIBRATE_FINISH) == true) {
                    Vibrator v = (Vibrator) discover.getSystemService(Context.VIBRATOR_SERVICE);
                    v.vibrate(ActivityDiscovery.VIBRATE);
                }
                discover.makeToast(R.string.discover_finished);
                discover.stopDiscovering();
            }
        }
    }

    @Override
    protected void onCancelled() {
        if (mDiscover != null) {
            final ActivityDiscovery discover = mDiscover.get();
            if (discover != null) {
                discover.makeToast(R.string.discover_canceled);
                discover.stopDiscovering();
            }
        }
        super.onCancelled();
    }
}

2 个答案:

答案 0 :(得分:0)

要取消你的asynctask只需偶尔检查一下isCancelled():

 protected Object doInBackground(Object... x) {
    while (/* condition */) {
      // work...
      if (isCancelled()) break;
    }
    return null;
 }

即使发出了一些请求,您的异步任务也会等到它完成并再次进行检查

要设置取消,您只需要获取asynctask实例并使用方法.cancel(boolean)

答案 1 :(得分:0)

这将取决于您将注意到您必须停止AsyncTask的事件,但通常最受欢迎的方法是将Boolean扩展程序设置为AsyncTasktruefalse),以及doInBackground()方法中将检查该变量并在该方法上调用return的过程。

所以,像这样:

public abstract class AbstractDiscovery extends AsyncTask<Void, HostBean, Void> {
  Boolean running = true;

  protected Void doInBackground(Void... params) {
    while (running) {
      // Do your stuff
      ...

      if (whatEverYouNeedToHappen() == true)
        running = false;
    }

    return null;
  }
}

没有“更直接”的方式来终止这项工作而不会因良好做法而崩溃。