0
我正在嘗試使用線程來實現並行下載,但是我的所有解壓縮過程都應該按順序進行。衆所周知,Intent Service會將所有待處理任務列入隊列,以便在所有下載內容結束時嘗試啓動一個意向服務。問題是,我發現了以下錯誤:是否可以從線程啓動一個意向服務?
05-30 11:49:10.520: E/AndroidRuntime(18790): FATAL EXCEPTION: main
05-30 11:49:10.520: E/AndroidRuntime(18790): java.lang.RuntimeException: Unable to instantiate service br.com.facilit.target.app.android.util.UnzipService: java.lang.InstantiationException: can't instantiate class br.com.facilit.target.app.android.util.UnzipService; no empty constructor
我的下載主題:
public class DownloadService implements Runnable {
Activity controller;
boolean post;
String urlParent;
String filePath;
String destinationPath;
ResultReceiver mReceiver;
String typeDownload;
MetaDados metaDado;
int index;
boolean isResuming;
File jsonFile = new File(Constants.DEST_PATH_PARENT + Constants.JSON_FILES_PATH);
File jsonTempFile;
public DownloadService(Activity controller, boolean post, String urlParent, String filePath,
String destinationPath, ResultReceiver mReceiver, String typeDownload, int index, MetaDados metaDado,
boolean isResuming) {
this.controller = controller;
this.post = post;
this.urlParent = urlParent;
this.filePath = filePath;
this.destinationPath = destinationPath;
this.mReceiver = mReceiver;
this.typeDownload = typeDownload;
this.index = index;
this.metaDado = metaDado;
this.isResuming = isResuming;
}
@Override
public void run() {
Log.d(Constants.DOWNLOAD_AND_UNZIP_SERVICE, "Começando processo de download");
// ALL DOWNLOAD PROCESS
// THEN CALL INTENT FOR UNZIP
final Intent service = new Intent(controller.getApplicationContext(), UnzipService.class);
service.putExtra("post", false);
service.putExtra("filePath", filePath);
service.putExtra("destinationPath", destinationPath);
service.putExtra("receiver", mReceiver);
service.putExtra("typeDownload", Constants.HTML);
service.putExtra("metaDado", metaDado);
service.putExtra("isResuming", false);
controller.runOnUiThread(new Runnable() {
@Override
public void run() {
controller.startService(service);
}
});
}
}
我解壓意圖服務:
public class UnzipService extends IntentService {
public UnzipService(String name) {
super("UnzipService");
}
@Override
protected void onHandleIntent(Intent intent) {
String filePath = intent.getStringExtra("filePath");
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
Log.d("UnzipService", "Simulando descompactação de arquivo " + filePath);
} catch (InterruptedException e) {
}
}
}
}
清單:
<service android:name="br.com.facilit.target.app.android.util.UnzipService"/>