java - Android 处理程序和线程在启动时未运行?

标签 java android multithreading handler runnable

我正在尝试在应用程序开始时创建一个处理程序,这样我就可以有两个线程工作:1 个 UI 和 2 个我的服务器,我这样做是为了让服务器不会阻止 UI 滞后,并且很有用解决我的滞后问题,但无论如何,我正在查看这个网站 http://crodrigues.com/updating-the-ui-from-a-background-thread-on-android/ ,这家伙创建了一个可运行的方法,有一个 run 方法,还有一个名为 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 只运行了一次。任何人都可以看到为什么它不继续在后台运行的问题吗?

Canvas

更新帖子

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

最佳答案

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

关于java - Android 处理程序和线程在启动时未运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15841644/

相关文章:

java - 如何在intellij上正确编译jnativehook?

android - 在android中的图表上创建线条 slider

java - 线程等到所有线程使用 java 返回输出?

C++0x "Hello Concurrent World"在 g++/linux 上立即出现段错误?

java - 在 SurfaceView 的这个简短示例中,哪个线程被阻塞等待线程 join() 完成?

java - Java/Clojure 中的 XML 解析 - 使用 XSD 进行类型强制转换?

java - 创建jar文件时如何添加输入文件

java - 如何从谷歌云端点获取远程IP地址?

android - 使用 Kotlin 在 .ics 文件中查找一行

java - 如何自动缩放图形以使其适合 Y 轴?