Proper way to wait end of a service and singleton design My code works fine and I would like to know if I chose the best strategy to implement my code, or if It can written better (more fast, more clear, best use of design pattern, etc).

In my android application WelcomeActivity, in the onCreate method, I start a really simple service that load data from a file:

public class LoadDataService extends IntentService {
    private int result;
    @Override
    protected void onHandleIntent(Intent intent) {
        try 
        {
            FileTestSingleton.getInstance(); //this could be long, read from file
            result = Activity.RESULT_OK;
        } catch (Exception e) 
        {
            Log.e(LoadDataService.class.getName(), "FILE DATA LOADING ERROR \n" + e);
            result = Activity.RESULT_CANCELED;
        }
        publishResults(result);
    }

    private void publishResults(int result) {
        stopSelf();
    }
} 


Some activity after, the user click a button to go to the next step of my APP. The onCreate method of MyNewActivity need the data from the service, otherwise it's ok to freeze the application, because if the data aren't loaded, nothing can be done. For example, I read that to stop the UI thread is a bad thing, but I need to stop it, otherwise my new activity will not have data to show.

So in my onCreate:

while (FileTestSingleton.getState()==Semaphore.LOADING)
{
    Thread.sleep(250);          
}
fileTest = FileTestSingleton.getInstance();


Is this a proper way to wait?

Semaphore is a simple enum that I created inside the FileTestSingleton class:

public enum Semaphore
{
    EMPTY,
    LOADING,
    DONE;
}
private static Semaphore semaphore = Semaphore.EMPTY;
private static FileTestSingleton fileTest;
public static FileTestSingleton getInstance() throws Exception
{
    if (fileTest==null &amp;&amp; semaphore!=Semaphore.LOADING)
    {
        semaphore = Semaphore.LOADING;
        fileTest = new FileTestSingleton(); //read from file, really slow
        semaphore = Semaphore.DONE;
    }
    return fileTest;
}
public static Semaphore getState()
{
    return semaphore;
}


Why I think that I can't use AsyncTask:
The problem is: I load some txt files on the startup in the WelcomeActivity that is for 2 seconds on the screen. After the app go in the MiddleActivity and the user  in the meantime can choose some options before he starts the final step of the application: FinalActivity. This FinalActivity need that all the txt files are loaded in the singleton.
Bug?

The catch block is a bit suspicious here:


try 
{
    FileTestSingleton.getIstance(); //this could be long, read from file
} catch (Exception e) 
{
    Log.e(LoadDataService.class.getName(), "FILE DATA LOADING ERROR \n" + e);
    result = Activity.RESULT_CANCELED;
}
result = Activity.RESULT_OK;
publishResults(result);



When you catch the exception, you set result = Activity.RESULT_CANCELED;,
but execution continues and on the next line comes result = Activity.RESULT_OK;.
In short, the method will return with success as if nothing happened.

Don't catch Exception

Always catch the most specific Exception type possible.
By catching something overly generic,
you may not notice when something truly unexpected is thrown.
In most practical situations there is a more specific exception class that you should check.

Don't declare a method to throw Exception

A method that's declared to throw Exception is very poorly designed.
It gives no clue to the caller about the kind of failure that can happen,
and it's impossible to know how to recover from it.
Surely you can think of something more specific,
to give the chance to callers to handle the failure gracefully.

Coding style

There is a standard in Java for placing braces:
the opening braces should be on the same line as their statement. Follow this style:

try {
    // ...
} catch (Exception e) {
    // ...
}


Proper way to wait for end of X

In other words, the proper way to execute a long operation without freezing the client. I think it's using an AsyncTask, with a ProgressDialog. Here's a rough sketch:

class WaitForServiceAsyncTask extends AsyncTask&lt;Void, Integer, Void&gt; {
    ProgressDialog mProgressDialog;

    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(YourActivity.this, "",
                YourActivity.this.getString(R.string.msg_waiting_for_service), false, true);
    }

    protected Void doInBackground(Void... params) {
        // the long task -- calling the service
    }
}


This won't freeze the UI thread and will let the user know that the app is not dead, but actually doing something.