I’m really confused about this, but when I’m trigging a thread from my SurfaceView, im sending a Handler with the constructor like this
private static Thread thread;
public SurfaceView(Context localContext) {
//other stuff
thread = new Thread(mySurfaceHolder, myEngine, this, new Handler());
//other stuff
}
and in my thread-class I assign a Handler-object with the handler I sent from my view, like this:
public Thread (SurfaceHolder lHolder,
Engine lEngine,
View lView,
Handler lHandler){
surfaceHolder = lHolder;
engine = lEngine;
view = lView;
handler = lHandler;
}
So what does this Handler do? I never use it in my thread-class in any ways, so why are the examples on the web still showing me that I should send a handler with the constructor?
I can’t see the connection.
I assume in your code you are extending Thread, because there is no constructor taking a handler for the Thread class. The handler is used to communicate back to the UI thread, as the Android threading model requires UI operations to be performed on a special dedicated thread.
When creating a new thread, the code is executed in a background thread, and thus UI-operations are unsafe. If you do not need to do anything UI-related, you can remove the handler.
Since Android API 3, there is AsyncTask, which makes communication between background and UI easier and alleviates the need to use a handler.
Examples:
see this.
Threading as well, which contains links to sample projects using threading.