将参数传递给 Asynctask

发布于 2024-12-02 13:45:56 字数 1176 浏览 0 评论 0原文

我正在使用异步任务从菜单活动中获取字符串并加载一些东西..但我是 无法这样做..我是否以正确的方式使用它并且我是否正确传递参数? 请查看代码片段。谢谢

  private class Setup extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {

            if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

I am using Async tasks to get string from the menu activity and load up some stuff..but i am
not able to do so..Am i using it in the right way and am i passing the parameters correctly?
Please see the code snippet. thanks

  private class Setup extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {

            if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲喜皆因你 2024-12-09 13:45:56

避免添加构造函数。

只需在任务执行方法中传递参数即可

new BackgroundTask().execute(a, b, c); // can have any number of params

现在您的后台类应该如下所示

public class BackgroundTask extends AsyncTask<String, Integer, Long> {

    @Override
    protected Long doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String a = arg0[0];
        String b = arg0[1];
        String c = arg0[2];
        //Do the heavy task with a,b,c
        return null;
    }
    //you can keep other methods as well postExecute , preExecute, etc

}

Avoid adding a constructor.

Simply pass your paramters in the task execute method

new BackgroundTask().execute(a, b, c); // can have any number of params

Now your background class should look like this

public class BackgroundTask extends AsyncTask<String, Integer, Long> {

    @Override
    protected Long doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String a = arg0[0];
        String b = arg0[1];
        String c = arg0[2];
        //Do the heavy task with a,b,c
        return null;
    }
    //you can keep other methods as well postExecute , preExecute, etc

}
春夜浅 2024-12-09 13:45:56

相反,我会

 private class Setup extends AsyncTask<String, Integer, Void> {

    @Override
    protected Void doInBackground(String... params) {
    String identifier = params[0];

          if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

在调用 asynctask 之前检查“标识符”,以防止创建

这样的AsyncTask 的开销

if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
               new Setup().execute(identifier);
    }

Instead of this i would do

 private class Setup extends AsyncTask<String, Integer, Void> {

    @Override
    protected Void doInBackground(String... params) {
    String identifier = params[0];

          if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

and check for "identifier" before invoking the asynctask to prevent overhead of creating a AsyncTask

like this

if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
               new Setup().execute(identifier);
    }
離殇 2024-12-09 13:45:56

一个简单的方法是添加一个构造函数:

public Setup(String a, Int b) {
    this.a = a;
    this.b = b;
}

A simple way is to add a constructor:

public Setup(String a, Int b) {
    this.a = a;
    this.b = b;
}
花想c 2024-12-09 13:45:56

AsyncTask 意味着 doInBackground() 返回 Void,onProgressUpdate() 接受整数参数,而 doInbackground 接受...字符串参数!

因此,您不需要(而且真的不应该)使用 Intent,因为它旨在用于通过活动而不是线程传递参数。

如前所述,您可以为您的类创建一个构造函数和一个名为“标识符”的全局参数

public class Setup...
{
    private String identifier;

    public Setup(String a) {
    identifier = a;
    }
}

希望它能有所帮助。
问候

AsyncTask means doInBackground() returns Void, onProgressUpdate() takes Integer params and doInbackground takes... String params !

So you don't need (and REALLY shouldn't) use Intent, since it is meant to be used for passing arguments through Activities, not Threads.

And as told before, you can make a constructor and a global parameter to your class called "identifier"

public class Setup...
{
    private String identifier;

    public Setup(String a) {
    identifier = a;
    }
}

Hoped it could help.
Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文