AsyncTask参数和使用" ..."在Java中

时间:2015-03-19 18:39:02

标签: android android-asynctask

                    void saveSnapshot(final SnapshotMetadata snapshotMetadata) {
                    AsyncTask<Void, Void, Snapshots.OpenSnapshotResult> task =
                            new AsyncTask<Void, Void, Snapshots.OpenSnapshotResult>() {
                                @Override
                                protected Snapshots.OpenSnapshotResult doInBackground(Void... params) {
                                    if (snapshotMetadata == null) {
                                        Log.i(TAG, "Calling open with " + currentSaveName);
                                        return Games.Snapshots.open(mGoogleApiClient, currentSaveName, true)
                                                .await();
                                    }
                                    else {
                                        Log.i(TAG, "Calling open with " + snapshotMetadata);
                                        return Games.Snapshots.open(mGoogleApiClient, snapshotMetadata)
                                                .await();
                                    }
                                }

                                @Override
                                protected void onPostExecute(Snapshots.OpenSnapshotResult result) {
                                    Snapshot toWrite = processSnapshotOpenResult(RC_SAVE_SNAPSHOT, result, 0);
                                    if (toWrite != null) {
                                        Log.i(TAG, writeSnapshot(toWrite));
                                    }
                                    else {
                                        Log.e(TAG, "Error opening snapshot: " + result.toString());
                                    }
                                }
                            };

                    task.execute();
                }

我理解正在创建一个AsyncTask对象。我从文档中看到参数可以根据需要进行更改或定义。我可以使用更多解释确切地说明为什么前两个参数将被声明为Void,Void。还有doInBackground params类型是一个Void ......?使用“......”是否有意义,例如普通的“虚空”和“虚空......”之间可能存在什么区别。

我期待任何回复或评论。我从CollectAllTheStars Google Play游戏服务基本样本中获取的代码。

谢谢。

3 个答案:

答案 0 :(得分:4)

3个泛型用于指定哪些类型转到doInBackground()的{​​{1}},onProgressUpdate()onPostExecute()方法。这允许您指出AsyncTask处理哪些特定类型的对象(AsyncTask),用于进度更新(Params)并获取最终结果({{1} })。它使用Progress的原因是由于变量参数:您可以在API中传递多个参数和进度报告。使用Result,因为它是一个适当的对象,表明缺少一个真实的对象(即装箱)。

答案 1 :(得分:1)

Java中的三个点(...)表示Vararg,这意味着您可以将零个或多个对象(作为数组)传递到方法中(或AsyncTask或其他)。他们在这里解释得非常好:

Java, 3 dots in parameters

答案 2 :(得分:1)

我认为这里令人困惑的部分是泛型为某些方法定义参数类型,以及同时为另一个方法定义返回类型。而且我们也从不直接调用AsyncTask覆盖的方法,而是通过我们调用的其他方法传递参数。许多示例使用<Void, Void, Integer>并不能帮助解决第一种和第二种类型的问题。

这就是为什么我想除了其他答案之外还要引用一些注释的示例代码。

注意,忽略varargs:

  1. onPostExecute(ResultClass result)的参数类型与ResultClass doInBackground(BackgroundParameterClass... parameters)
  2. 的返回类型相同
  3. publishProgress(ProgressClass progress)的参数类型与onProgressUpdate(ProgressClass... values)
  4. 的参数类型相同
  5. 参数类型execute(BackgroundParameterClass backgroundParameter);与参数类型doInBackground(BackgroundParameterClass... params)

    相同
    private static class BackgroundParameterClass {};
    private static class ProgressClass {};
    private static class ResultClass {};
    
    /**
     * This AsyncTask could for example download a image from a web server
     */
    private static class MyAsyncTask extends AsyncTask<BackgroundParameterClass, ProgressClass, ResultClass> {
    
        @Override
        protected void onPreExecute() {
            // this is called on the UI thread
            // do anything you need to do before the background word start
            // e.g. disable the download button
        }
    
        @Override
        protected ResultClass doInBackground(BackgroundParameterClass... params) {
            // Do some background work here, for example in a loop
            // call then publishProgress(B... values)
            // e.g download the image from a server
            for (int index = 0; index < 10; index++) {
                // download the image in chunks
                ProgressClass progress = new ProgressClass();
                publishProgress(progress);
            }
            ResultClass result = new ResultClass();
            return result;
        }
    
        @Override
        protected void onProgressUpdate(ProgressClass... values) {
            // this is called on the UI thread
            // e.g. update a loading bar
        }
    
        @Override
        protected void onPostExecute(ResultClass resultClass) {
            // this is called on the UI thread
            // e.g. display the image in your UI
        }
    }
    
  6. 然后通过调用

    获取MyAsyncTask
    new MyAsyncTask().execute(new BackgroundParameterClass());