我的asynctask中有以下代码:

@Override
protected void onPreExecute() {
    waitDialog = new ProgressDialog(context);
    waitDialog.setTitle(R.string.wait_title);
    waitDialog.setMessage(context.getString(R.string.wait_body));
    waitDialog.setIndeterminate(true);
    waitDialog.show();
}

@Override
protected Boolean doInBackground(Void... unused) {
    Intent serviceIntent = new Intent(context, PublisherService.class);
    saveSettings(false);
    startService(serviceIntent);
    return serviceIsRunning();
}


该对话框显示,但是在我的服务启动时(需要几秒钟),进度对话框被冻结。如果我在SystemClock.sleep()调用中使用一个简单的doInBackground(),它可以正常工作。

有人可以告诉我为什么会这样以及如何解决问题吗?

谢谢

最佳答案

您不应从线程或异步任务启动任何服务。服务可以启动自己的线程并在那里执行所有工作。也可以在onPreExecute()中绑定到服务,然后从doInBackground调用其方法。在此处阅读本地服务样本http://developer.android.com/reference/android/app/Service.html

关于android - 尝试使用asynctask运行服务时ProgressDialog卡住,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4184968/

10-09 07:24
查看更多