アプリケーションの開始時にハンドラーを作成しようとしているので、1 つの UI と 2 つのサーバーで 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();
}
}