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游戏服务基本样本中获取的代码。
谢谢。
答案 0 :(得分:4)
3个泛型用于指定哪些类型转到doInBackground()
的{{1}},onProgressUpdate()
和onPostExecute()
方法。这允许您指出AsyncTask
处理哪些特定类型的对象(AsyncTask
),用于进度更新(Params
)并获取最终结果({{1} })。它使用Progress
的原因是由于变量参数:您可以在API中传递多个参数和进度报告。使用Result
,因为它是一个适当的对象,表明缺少一个真实的对象(即装箱)。
答案 1 :(得分:1)
Java中的三个点(...)表示Vararg,这意味着您可以将零个或多个对象(作为数组)传递到方法中(或AsyncTask
或其他)。他们在这里解释得非常好:
答案 2 :(得分:1)
我认为这里令人困惑的部分是泛型为某些方法定义参数类型,以及同时为另一个方法定义返回类型。而且我们也从不直接调用AsyncTask覆盖的方法,而是通过我们调用的其他方法传递参数。许多示例使用<Void, Void, Integer>
并不能帮助解决第一种和第二种类型的问题。
这就是为什么我想除了其他答案之外还要引用一些注释的示例代码。
注意,忽略varargs:
onPostExecute(ResultClass result)
的参数类型与ResultClass doInBackground(BackgroundParameterClass... parameters)
publishProgress(ProgressClass progress)
的参数类型与onProgressUpdate(ProgressClass... values)
参数类型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
}
}
然后通过调用
获取MyAsyncTasknew MyAsyncTask().execute(new BackgroundParameterClass());