可以从线程启动Intent服务吗?

时间:2013-05-30 14:58:42

标签: android multithreading intentservice

我正在尝试使用Thread实现并行下载,但我的所有解压缩过程应该是顺序的。众所周知,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);

                    }
                });
    }
}

My Unzip Intent 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"/>

1 个答案:

答案 0 :(得分:3)

作为异常报告,您no empty constructor将其更改为:

public UnzipService() {
        super("UnzipService");
 }