Search code examples
javaandroidmultithreadinghandlerrunnable

Android Handler and Thread at start time not running?


I'm trying to create a handler at the start of my application so I can have two threads working 1 the UI and 2 my server, I'm doing this so the server will not stop the UI from lagging, and usefully sort out my lag issues, but anyways, I'm looking at this website http://crodrigues.com/updating-the-ui-from-a-background-thread-on-android/ , the guy creates a runnable method, with a run method, there is also a method called updateGame which is always called when that method is ran, now I have tried out his code like so

public class MainActivity extends Activity {

private static final String TAG = gameObject.class.getSimpleName();
//Create a handler to deal with the server
private Handler serverHandler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Turn off title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Make the application full screen
    getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView( new gamePanel( this ) );
    Log.d( TAG, "View added" );

    //Server method
    new Thread(new Runnable() { onServer( ); } ).start( );
}

final Runnable updateRunnable = new Runnable() {
    public void run() {
        //call the activity method that updates the UI
        updateGame();
    }
};

//Give the positions to the game
public void updateGame()
{
    Log.d(TAG, "Update that game");
}

//Update/run the server
private void onServer()
{
    if( gamePanel.rtnServerState() == true )
    {
        Log.d(TAG, "Start the server");
    }

    serverHandler.post( updateRunnable );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onDestroy()
{
    Log.d( TAG,  "Destroying... " );
    super.onDestroy();
}

public void onStop()
{
    Log.d( TAG,  "Stopping... " );
    super.onStop();
}
}

and my updateGame is only ran once. Can anyone see the issue in why it doesn't keep running in the background?

Canvas

Updated post

public class MainActivity extends Activity {

private static final String TAG = gameObject.class.getSimpleName();
private final Handler serverHandler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Turn off title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Make the application full screen
    getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView( new gamePanel( this ) );

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    String message = "hello";
    textView.setText(message);

    Log.d( TAG, "View added" );
    //Server method
    new Thread(new Runnable() { 

    @Override
    public void run() { onServer( ); } } ).start( );
}

private void updateServer()
{
    Log.d(TAG, "testing");
}


//Update/run the server
private void onServer()
{
    if( gamePanel.rtnServerState() == true )
    {
        Log.d(TAG, "Start the server");
    }

    serverHandler.post( updateRunnable );
}

//Update/server
final Runnable updateRunnable = new Runnable() {
    public boolean running = true;
    public void run() {
        while(running){
            //call the activity method that updates the UI
            updateServer();
        }
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onDestroy()
{
    Log.d( TAG,  "Destroying... " );
    super.onDestroy();
}

public void onStop()
{
    Log.d( TAG,  "Stopping... " );
    super.onStop();
}
}

Update number 2

public class MainActivity extends Activity {

private static final String TAG = gameObject.class.getSimpleName();
private final Handler serverHandler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Turn off title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Make the application full screen
    getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView( new gamePanel( this ) );

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    String message = "hello";
    textView.setText(message);

    Log.d( TAG, "View added" );
    //Server method
    Runnable server = new Runnable() {
        public boolean running = true;
        public void run() {
            while(running){
                onServer();  // Make sure this blocks in some way
            }
        }
    };
}

private void updateServer()
{
    Log.d(TAG, "testing");
}


//Update/run the server
private void onServer()
{
    if( gamePanel.rtnServerState() == true )
    {
        Log.d(TAG, "Start the server");
    }

    serverHandler.post( updateRunnable );
}

//Update/server
final Runnable updateRunnable = new Runnable() {
    public void run() {
        //call the activity method that updates the UI
        updateServer();
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onDestroy()
{
    Log.d( TAG,  "Destroying... " );
    super.onDestroy();
}

public void onStop()
{
    Log.d( TAG,  "Stopping... " );
    super.onStop();
}
}

Solution

  • The Runnable object's run method is only called once, after a new Thread is created in response to your .start() call.

    Usually you do something like:

    final Runnable myRunnable = new Runnable() {
        public boolean running = true;
        public void run() {
            while(running){
                doSomething();
            }
        }
    };
    

    But I'm not sure this is the best way to do this. The updateGame() method will be constantly called unnecessarily.

    Instead, put your server logic inside the runnable's run() method. In there use the while(running){...} construct I listed above but make sure there is some blocking call in there. Whether it be from a network socket, a BlockingQueue, etc. That way it won't needlessly loop.


    EDIT

    From discussion in comments. Leave

    final Runnable updateRunnable = new Runnable() {
        public void run() {
            //call the activity method that updates the UI
            updateGame();
        }
    };
    

    as it is and change

    new Thread(new Runnable() { onServer( ); } ).start( );
    

    to

    Runnable server = new Runnable() {
        public boolean running = true;
        public void run() {
            while(running){
                onServer();  // Make sure this blocks in some way
            }
        }
    }
    new Thread(server).start();