开发者

ProgressDialog freezes when trying to run a service with an asynctask

I have this code inside my asynctask:

@Override
protected void onPreExecute() {
    waitDialog = new ProgressDia开发者_C百科log(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();
}

The dialog shows but while my service is starting (and it takes a few seconds) the progressdialog is frozen. If i use a simple SystemClock.sleep() inside my doInBackground() call it works fine.

Can someone tell me why is this happening and how to solve the issue?

Thanks


You should not start any services from threads or asynctasks. Service could start its own thread and perform all work there. Or it's possible to bind to the service in onPreExecute() and call its methods from doInBackground. Read Local Service Sample here http://developer.android.com/reference/android/app/Service.html


If you use SystemClock.sleep() where? You mean instead of creating a service? Well first of all services and any other activity is created on the UI thread. So starting your service in an asynctask does nothing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜