AsyncTask,多个不同的操作 [英] AsyncTask, multiple different operations

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

问题描述

我目前有一种使用4种方法的课程.我需要将其更改为AsyncTask.每个方法都接收不同的参数(File,int,String ...)以供使用,并通过post或get连接到不同的URL.我的问题是,我仍然可以将所有这些操作都包含在一个AsyncTask类中,还是需要为每个方法创建一个新的AsyncTask类?

I currently have one class with 4 methods. I need to change that to AsyncTask. Every method receives different parameters (File, int, String ...) to work with and connects to different URL with post or get. My question is can I still somehow have all those operations in one AsyncTask class or I will need to create new AsyncTask class for every method?

private class Task extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
 }

推荐答案

这取决于您是否需要同时运行所有4个AsyncTask还是可以依次运行.

This depends if you need all 4 AsyncTasks to run simultaneously or if they can run sequentially.

我可以想象它们可以按顺序运行,因为这就是它们当前在Main线程中的运行方式,因此只需传递所有需要的参数并一一执行它们的操作即可.实际上,如果已经编写了函数,只需将这些函数移到您的AsyncTask类中即可:

I would imagine they can run sequentially since that's how they are running currently in the Main thread, so just pass all the needed parameters and execute their operations one by one. In fact, if the functions are already written, just move those functions into your AsyncTask class:

MainActivity.java:

MainActivity.java:

public static final int FILE_TYPE = 0;
public static final int INT_TYPE = 1;
public static final int STRING_TYPE = 2;

taskargs = new Object[] { "mystring", new File("somefile.txt"), new myObject("somearg") };

new Task(STRING_TYPE, taskargs).execute();

AsyncTask

AsyncTask

private class Task extends AsyncTask<URL, Integer, Long> {
    private Int type;
    private Object[] objects;
    public Task(Int type, Object[] objects) {
        this.type = type;
        this.objects = objects;
    }
    protected Long doInBackground(URL... urls) {
        int count = urls.length;
        long totalSize = 0;
        for (int i = 0; i < count; i++) {
        }
        //obviously you can switch on whatever string/int you'd like
        switch (type) {
            case 0:  taskFile();
                     break;
            case 1:  taskInteger();
                     break;
            case 2:  taskString();
                     break;
            default: break;
        }
        return totalSize;
    }

    protected void onProgressUpdate(Integer... progress) {
        setProgressPercent(progress[0]);
    }

    protected void onPostExecute(Long result) {
        showDialog("Downloaded " + result + " bytes");
    }
    protected void taskFile(){ //do something with objects array }
    protected void taskInteger(){ //do something with objects array }
    protected void taskString(){ //do something with objects array }
}

这篇关于AsyncTask,多个不同的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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