-
Notifications
You must be signed in to change notification settings - Fork 2
/
async_task_help.java
116 lines (94 loc) · 2.96 KB
/
async_task_help.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package pl.project13.activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import com.google.inject.Inject;
import pl.project13.R;
import pl.project13.lazy.Details;
import pl.project13.lazy.LazyWorker;
import roboguice.activity.RoboListActivity;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* @author Konrad Malawski
*/
public class TasksActivity extends RoboListActivity {
@Inject
LazyWorker lazyWorker;
Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Toast.makeText(this, "Hello, from " + this.getClass().getSimpleName(), Toast.LENGTH_LONG).show();
setContentView(R.layout.list);
// loadData();
asyncLoadData();
}
private void asyncLoadData() {
List<String> datas = Collections.emptyList();
try {
datas = new AsyncTask<Void, Integer, List<String>>() {
@Override
protected List<String> doInBackground(Void... voids) {
return lazyWorker.getData();
}
}.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
final ProgressDialog progressDialog = new ProgressDialog(TasksActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading details...");
progressDialog.setCancelable(false);
try {
final List<String> finalDatas = datas;
progressDialog.setMax(datas.size());
new AsyncTask<String, Integer, Void>() {
@Override
protected void onPreExecute() {
// handler.post(new Runnable() {
// public void run() {
progressDialog.show();
// }
// });
}
@Override
protected void onPostExecute(Void aVoid) {
// handler.post(new Runnable() {
// public void run() {
progressDialog.dismiss();
// }
// });
}
@Override
protected Void doInBackground(String... strings) {
int i = 1;
for (final String data : finalDatas) {
final Details details = lazyWorker.getDetails(data);
handler.post(new Runnable() {
public void run() {
Toast.makeText(TasksActivity.this, "processed: " + details.getId(), Toast.LENGTH_SHORT)
.show();
}
});
publishProgress(i++);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
}
}.execute(datas.toArray(new String[0]));
} finally {
progressDialog.hide();
}
}
private void loadData() {
List<String> data = lazyWorker.getData();
}
}