user1433460
user1433460

Reputation: 27

Android: UI freezes when intent calls a service

Hello I have an intent as below that starts a service

public void startAlarmService() {
    Intent i = new Intent();
    i.setAction("me.application.AlarmService");
    startService(i);
}

This method is called when a checkbox is checked. However, when it gets checked, the UI freezes for like 5 seconds..

I tried to call the above method by running a new Thread, and then by using AsyncTask but both didn't work and it took also like 5 seconds to run since for example I put a progress dialog and the UI froze before showing the progress dialog.

How can I make sure that the UI does not freeze on start this service?

Thank you for you help.

Edit: Service Code: Download part using async task:

asyncDownload aCall = new asyncDownload(getApplicationContext(),this);              
boolean result = aCall.execute().get();
return result;

Upvotes: 0

Views: 1350

Answers (2)

Fenix Voltres
Fenix Voltres

Reputation: 3448

You are firstly creating an AsyncTask and later using it's get() method, which blocks main thread. Do it properly - background work in doInBackground(Params...), then update UI in onPostExecute(Result), which is called in main thread again - instead of using get().

Upvotes: 0

Teovald
Teovald

Reputation: 4389

By default a service run on the same thread as the activities of the same app, ie the UI thread.
So running your service in a new thread looks like a good solution.

Upvotes: 1

Related Questions