我试图在我的应用程序开始时创建一个处理程序,这样我就可以让两个线程工作 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();
}
}