Android在AsyncTask上发送自定义参数

时间:2014-12-31 09:10:25

标签: java android android-asynctask

大家都通过以下方法我可以毫无问题地发送任何自定义参数。现在我想将此方法更改为名称为LongOperation的AsyncTask类,但我不知道如何将getRequestFromServer方法参数发送到AsyncTask

我的方法是:

public static void getRequestFromServer(final long lastID, final int count,
                                        final ContentResolver cr){

我想要这样:

private class LongOperation extends AsyncTask<final long lastID, final int count,
                                            final ContentResolver cr> {

2 个答案:

答案 0 :(得分:1)

private class LongOperation extends AsyncTask<Void, Void, Void> {

    long lastID;
    int count;
    ContentResolver cr;

    public LongOperation(final long lastID, final int count,
                                    final ContentResolver cr){
        this.lastID = lastID;
        this.count = count;
        this.cr = cr;
    }

使用它:

new LongOperation(LAST_ID,COUNT,CR).execute();

答案 1 :(得分:0)

另一种方法是将参数传递给doInBackground方法,如下所示

  private static class MyTaskParams {
    long lastID;
    int count;
    ContentResolver cr ;

    MyTaskParams(long id, int count, ContentResolver cr) {
        this.lastID; = id;
        this.count = count;
        this.cr = cr;
    }
    }
      @Override
        protected void doInBackground(MyTaskParams... params) {
           long lastID= params[0].lastID;
            int  count= params[0].count;
            ContentResolver cr= params[0].cr;
            ...
        }

要调用你的Asynctaks,你必须编写以下代码:new LongOperation()。execute(passing);