我的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();
}
对话框显示但是当我的服务启动时(需要几秒钟),progressdialog被冻结。如果我在SystemClock.sleep()
调用中使用简单doInBackground()
,则可以正常使用。
有人可以告诉我为什么会这样,以及如何解决这个问题?
由于
答案 0 :(得分:1)
您不应该从线程或asynctasks启动任何服务。服务可以启动自己的线程并在那里执行所有工作。或者可以在onPreExecute()中绑定到服务并从doInBackground调用其方法。在此处阅读本地服务示例http://developer.android.com/reference/android/app/Service.html
答案 1 :(得分:0)
如果您使用SystemClock.sleep()
在哪里?你的意思是代替创建服务?首先,在UI线程上创建所有服务和任何其他活动。因此,在asynctask中启动服务不会做任何事情。