@@ -144,3 +144,98 @@ private class DownloadTask extends AsyncTask<String, Integer, String> {
144
144
```
145
145
146
146
### 从服务器上下载文件
147
+
148
+ 这里有个最大的问题:* 我怎么从service来更新我的activity?* 。
149
+ 在下一个例子当中我们会使用两个你可能不熟悉的类:`ResultReceiver `和`IntentService `。`ResultReceiver `是一个可以允许我们用Service 来更新线程的类;`IntentService `是一个可以生成用来处理后台任务的线程的`Service `子类(你需要知道,`Service `实际上是和你的应用运行在同一个线程的;当你继承了`Service `之后,你必须手动生成一个新的线程来处理费时操作)。
150
+
151
+ 一个提供下载功能的`Service `看起来像这样:
152
+
153
+ ```java
154
+ public class DownloadService extends IntentService {
155
+ public static final int UPDATE_PROGRESS = 8344 ;
156
+ public DownloadService () {
157
+ super (" DownloadService" );
158
+ }
159
+ @Override
160
+ protected void onHandleIntent (Intent intent ) {
161
+ String urlToDownload = intent. getStringExtra(" url" );
162
+ ResultReceiver receiver = (ResultReceiver ) intent. getParcelableExtra(" receiver" );
163
+ try {
164
+ URL url = new URL (urlToDownload);
165
+ URLConnection connection = url. openConnection();
166
+ connection. connect();
167
+ // 这对你在进度条上面显示百分比很有用
168
+ int fileLength = connection. getContentLength();
169
+
170
+ // download the file
171
+ InputStream input = new BufferedInputStream (connection. getInputStream());
172
+ OutputStream output = new FileOutputStream (" /sdcard/BarcodeScanner-debug.apk" );
173
+
174
+ byte data[] = new byte [1024 ];
175
+ long total = 0 ;
176
+ int count;
177
+ while ((count = input. read(data)) != - 1 ) {
178
+ total += count;
179
+ // 更新进度条....
180
+ Bundle resultData = new Bundle ();
181
+ resultData. putInt(" progress" ,(int ) (total * 100 / fileLength));
182
+ receiver. send(UPDATE_PROGRESS , resultData);
183
+ output. write(data, 0 , count);
184
+ }
185
+
186
+ output. flush();
187
+ output. close();
188
+ input. close();
189
+ } catch (IOException e) {
190
+ e. printStackTrace();
191
+ }
192
+
193
+ Bundle resultData = new Bundle ();
194
+ resultData. putInt(" progress" ,100 );
195
+ receiver. send(UPDATE_PROGRESS , resultData);
196
+ }
197
+ }
198
+ ```
199
+
200
+ 把这个`Service `添加到清单文件中:
201
+ ```
202
+ < service android: name= " .DownloadService" / >
203
+ ```
204
+
205
+ activity里面的代码:
206
+
207
+ ```java
208
+ // 像第一个例子里面一样初始化ProgressBar
209
+
210
+ // 在这里启动下载
211
+ mProgressDialog.show ();
212
+ Intent intent = new Intent (this , DownloadService . class);
213
+ intent.putExtra ("url ", "url of the file to download ");
214
+ intent.putExtra ("receiver ", new DownloadReceiver (new Handler ()));
215
+ startService (intent );
216
+ ```
217
+
218
+ 然后像这样来使用`ResultReceiver `:
219
+
220
+ ```java
221
+ private class DownloadReceiver extends ResultReceiver {
222
+ public DownloadReceiver (Handler handler ) {
223
+ super (handler);
224
+ }
225
+
226
+ @Override
227
+ protected void onReceiveResult (int resultCode , Bundle resultData ) {
228
+ super . onReceiveResult(resultCode, resultData);
229
+ if (resultCode == DownloadService . UPDATE_PROGRESS ) {
230
+ int progress = resultData. getInt(" progress" );
231
+ mProgressDialog. setProgress(progress);
232
+ if (progress == 100 ) {
233
+ mProgressDialog. dismiss();
234
+ }
235
+ }
236
+ }
237
+ }
238
+ ```
239
+
240
+ #### 使用Groundy 库
241
+ [Groundy](http:// casidiablo.github.com/groundy)
0 commit comments