Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android处理程序和线程在启动时未运行?_Java_Android_Multithreading_Handler_Runnable - Fatal编程技术网

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

Java Android处理程序和线程在启动时未运行?,java,android,multithreading,handler,runnable,Java,Android,Multithreading,Handler,Runnable,我试图在我的应用程序开始时创建一个处理程序,这样我可以让两个线程工作1 UI和2我的服务器,我这样做是为了服务器不会阻止UI滞后,并有效地解决我的滞后问题,但无论如何,我在看这个网站,这个家伙创建了一个可运行的方法,带有一个运行方法,还有一个名为updateGame的方法,它总是在运行该方法时被调用,现在我已经像这样试用了他的代码 public class MainActivity extends Activity { private static final String TAG = gam

我试图在我的应用程序开始时创建一个处理程序,这样我可以让两个线程工作1 UI和2我的服务器,我这样做是为了服务器不会阻止UI滞后,并有效地解决我的滞后问题,但无论如何,我在看这个网站,这个家伙创建了一个可运行的方法,带有一个运行方法,还有一个名为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();
}
}
我的UpdateName只运行一次。有人能从为什么它不能一直在后台运行中看出这个问题吗

帆布

更新帖子

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();
}
}
在响应
.start()
调用创建新线程后,仅调用一次可运行对象的
run
方法

通常情况下,您会执行以下操作:

final Runnable myRunnable = new Runnable() {
    public boolean running = true;
    public void run() {
        while(running){
            doSomething();
        }
    }
};
但我不确定这是最好的方法。
updateGame()
方法将不断被不必要地调用

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


编辑

来自评论中的讨论。离开

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

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


在教程中,用户界面仅在单击按钮时更新,但在
onCreate
方法中仅更新一次。如果你用@jedwards的方式来做,你的用户界面就会像你写的那样冻结。不要总是更新用户界面。尝试使用或更新它。这将更加有效,您的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。

我这样做很好,但现在我的UI已冻结,我尝试的是允许UI线程运行,而这个新线程“工作线程”将处理所有网络处理,并有望阻止我的应用程序主机滞后,并导致网络数据包延迟3秒。我编辑了我的帖子。为什么不让“服务器/网络处理程序”成为一个线程,一旦它确定应该更新UI,就调用
serverHandler.post(new Runnable(){updateGame();})一开始游戏是没有网络的,一旦用户按下游戏主机,他们就变成了服务器,我的UpdateName()也不做任何事情,我正试图找出如何为UI启动一个线程并创建一个工作线程。你不能为UI启动一个线程。您的活动在UI线程上运行。您只需要为会影响UI响应的后台活动/事情启动一个线程,比如网络。在当前的Android API中,您必须在不同的线程上执行此操作,否则当您尝试运行应用程序时会出现异常。好的,但当我启动此线程时,它只会锁定我的应用程序的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);