2

我试图在我的应用程序开始时创建一个处理程序,这样我就可以让两个线程工作 1 UI 和 2 我的服务器,我这样做是为了服务器不会阻止 UI 滞后,并且有用地整理我的滞后问题,但无论如何,我正在查看这个网站http://crodrigues.com/updating-the-ui-from-a-background-thread-on-android/,这家伙创建了一个可运行的方法,运行方法,还有一个叫做 updateGame 的方法,它总是在运行该方法时被调用,现在我已经像这样尝试了他的代码

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();
}
}

而我的 updateGame 只运行一次。谁能看到为什么它不能在后台继续运行的问题?

帆布

更新帖子

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();
}
}

更新编号 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();
}
}
4

2 回答 2

2

Runnable对象run方法只被调用一次,在一个新的线程被创建以响应你的.start()调用之后。

通常你会做类似的事情:

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

但我不确定这是最好的方法。该updateGame()方法将不断被不必要地调用。

相反,将您的服务器逻辑放在可运行的run()方法中。在那里使用while(running){...}我上面列出的构造,但要确保那里有一些阻塞调用。无论是来自网络套接字、BlockingQueue 等。这样它就不会不必要地循环。


编辑

来自评论中的讨论。离开

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

照原样改变

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

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();
于 2013-04-05T19:11:29.773 回答
0

在教程中,UI 仅在按钮单击时更新,但您只在onCreate方法中执行一次。如果您以@jedwards 的方式执行此操作,您的 UI 将在您编写时冻结。不要一直更新 UI。尝试使用timer或使用sockets更新它。它会更有效,并且您的 UI 不会冻结。

定时器示例

import java.util.Timer;
import java.util.TimerTask;
...

TimerTask task = new TimerTask() {
    @Override
    public void run() {
        // update UI
    }
};
Timer timer = new Timer();
// 1000 - after 1 second run this timer
// 3000 - and do it every 3 second
timer.schedule(task, 1000, 3000);

我不会写套接字示例,因为要写的东西太多了。我找到了本教程,您可以在其中阅读有关 android 套接字编程的信息。

顺便说一句,当然您应该只使用套接字或计时器更新实体数据,而不是整个 UI。

于 2013-04-05T19:20:44.677 回答