Android AsyncTask参数和";的使用&引用;在爪哇 void saveSnapshot(最终快照元数据快照元数据){ 异步任务= 新建异步任务(){ @凌驾 受保护的快照。OpenSnapshotResult doInBackground(无效…参数){ if(快照元数据==null){ Log.i(标记“调用open并使用“+currentSaveName”); 返回游戏。快照。打开(mGoogleApiClient,currentSaveName,true) .等待(); } 否则{ Log.i(标记“调用open并使用“+快照元数据”); 返回游戏.快照.打开(mGoogleApiClient,快照元数据) .等待(); } } @凌驾 受保护的void onPostExecute(Snapshots.OpenSnapshotResult){ Snapshot toWrite=processSnapshotOpenResult(RC_SAVE_快照,结果,0); if(toWrite!=null){ Log.i(TAG,writeSnapshot(toWrite)); } 否则{ Log.e(标记“打开快照时出错:”+result.toString()); } } }; task.execute(); }

Android AsyncTask参数和";的使用&引用;在爪哇 void saveSnapshot(最终快照元数据快照元数据){ 异步任务= 新建异步任务(){ @凌驾 受保护的快照。OpenSnapshotResult doInBackground(无效…参数){ if(快照元数据==null){ Log.i(标记“调用open并使用“+currentSaveName”); 返回游戏。快照。打开(mGoogleApiClient,currentSaveName,true) .等待(); } 否则{ Log.i(标记“调用open并使用“+快照元数据”); 返回游戏.快照.打开(mGoogleApiClient,快照元数据) .等待(); } } @凌驾 受保护的void onPostExecute(Snapshots.OpenSnapshotResult){ Snapshot toWrite=processSnapshotOpenResult(RC_SAVE_快照,结果,0); if(toWrite!=null){ Log.i(TAG,writeSnapshot(toWrite)); } 否则{ Log.e(标记“打开快照时出错:”+result.toString()); } } }; task.execute(); },android,android-asynctask,Android,Android Asynctask,据我所知,正在创建AsyncTask对象。我从文档中看到,可以根据需要更改或定义参数。我可以用更多的解释来解释为什么前两个参数会被声明为Void,Void。以及doInBackground参数类型是否为Void。。。?例如,“Void”和“Void…”之间的区别是什么 我期待任何回应或评论。我从谷歌游戏服务基本样本中收集的代码 谢谢。Java中的三个点(…)表示Vararg,这意味着您可以将零个或多个对象(作为数组)传递到方法中(或AsyncTask或其他)。这里对它们的解释非常好: Java中

据我所知,正在创建AsyncTask对象。我从文档中看到,可以根据需要更改或定义参数。我可以用更多的解释来解释为什么前两个参数会被声明为Void,Void。以及doInBackground参数类型是否为Void。。。?例如,“Void”和“Void…”之间的区别是什么

我期待任何回应或评论。我从谷歌游戏服务基本样本中收集的代码

谢谢。

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

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


这3个泛型用于指定
异步任务的
doInBackground()
onProgressUpdate()
onPostExecute()
方法的类型。这允许您指示
AsyncTask
处理哪些特定类型的对象(
Params
),用于进度更新(
progress
),并作为最终结果获取(
result
)。它之所以使用
是因为变量参数:您可以在API中传递多个参数和进度报告
Void
是一个适当的对象,表示缺少真实对象(即装箱)。

这3个泛型用于指定
doInBackground()
onProgressUpdate()
onPostExecute()
异步任务的方法的类型。这允许您指示
AsyncTask
处理哪些特定类型的对象(
Params
),用于进度更新(
progress
),并作为最终结果获取(
result
)。它之所以使用
是因为变量参数:您可以在API中传递多个参数和进度报告
Void
被使用,因为它是一个适当的对象,表示缺少真实对象(即装箱)。

我认为这里令人困惑的部分是泛型定义了某些方法的参数类型,同时定义了另一个方法的返回类型。而且,我们从不直接调用AsyncTask覆盖的方法,而是通过我们调用的其他方法传递参数。许多示例使用
,其中第一种和第二种类型无法区分,这也没有帮助

这就是为什么除了其他答案之外,我还想加入一些注释过的示例代码

请注意,忽略varargs时:

  • onPostExecute(ResultClass结果)
    的参数类型与
    ResultClass doInBackground(BackgroundParameterClass…参数)
  • publishProgress(ProgressClass进度)
    的参数类型与
    onProgressUpdate(ProgressClass…值)
  • execute的参数类型(BackgroundParameterClass backgroundParameter)
    doInBackground(BackgroundParameterClass…params)


    我相信这里令人困惑的部分是泛型定义了一些方法的参数类型,同时也定义了另一个方法的返回类型。而且,我们从不直接调用AsyncTask覆盖的方法,而是通过我们调用的其他方法传递参数。许多示例使用
    ,其中第一种和第二种类型无法区分,这也没有帮助

    这就是为什么除了其他答案之外,我还想加入一些注释过的示例代码

    请注意,忽略varargs时:

  • onPostExecute(ResultClass result)
    的参数类型与
    resultla的返回类型相同
    
                        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();
                    }
    
    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
        }
    }
    
    new MyAsyncTask().execute(new BackgroundParameterClass());