私はそれを行うためにいくつかの方法を試しましたが、分離しませんでした。3 つのスレッドを開始する MainActivity があります。ユーザーが「戻る」ボタンを押したとき、または何らかの理由でアプリが停止したとき(電話など)にスレッドを停止したい。アクティビティが再び表示された後 (ユーザーがアプリに戻ったとき)、スレッドは停止した場所から続行されます。MainActivity で定義されたすべてのスレッドが開始されます。ありがとう!
public class MainActivity extends Activity
{
//threads
private PingPongGame gameThread;
private PaddleMover paddleMoverThread;
private PresentThread giftThread;
public GameSounds gameSounds;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
gameLevel = new GameLevel0(screenWidth , screenHeight, this.giftArr);
gameLevelView = new GameLevelView(this,gameLevel);
// Creating the game view
this.gameView = new PingPongView(this);
// Setting the gameView as the main view for the PingPong activity.
setContentView(gameView);
if(gameThread == null){
//create the main thread
gameThread = new PingPongGame( gamePaddle, gameView, gameLevel , message , ballArr , gameSounds);
//create the thread responsible for moving the paddle
paddleMoverThread = new PaddleMover(gamePaddle, gameView);
//create the thread responsible for present
giftThread = new PresentThread(gamePaddle , gameView , gameLevel, message , giftArr , ballArr,gameSounds );
gameThread.start();
paddleMoverThread.start();
giftThread.start();
}
}
//This method is automatically called when the user touches the screen
@Override
public boolean onTouchEvent(MotionEvent event)
{
float destination;
// Toast.makeText(this, "try!", Toast.LENGTH_SHORT).show();
//get the x coordinate of users' press
destination = event.getX();
//notify the paddle mover thread regarding the new destination
gamePaddle.setPaddleDestination(destination);
return true;
}
}
私のスレッドの例:
public class PaddleMover extends Thread
{
private Tray gamePaddle; //holds a reference to the paddle
private PingPongView gameView; //holds a reference to the main view
//for stop
private Object mPauseLock;
private boolean mPaused;
//initialize class variables
public PaddleMover(Tray thePaddle, PingPongView mainView)
{
gamePaddle = thePaddle;
gameView = mainView;
//for stop and resume threads
mPauseLock = new Object();
mPaused = false;
}
//main method of the current thread
@Override
public void run()
{
//infinitely loop, and move the paddle if necessary
while ((Const.isLose == false) && (Const.isCompleteThisLevel==false) && (Const.isDestroy == false))
{
//check whether the paddle should be moved
if (gamePaddle.getMiddle() != gamePaddle.getPaddleDestination())
{
//move the paddle
gamePaddle.move();
//send a request to refresh the display
gameView.postInvalidate();
}
try
{
PaddleMover.sleep(3);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//for stop and resume
synchronized (mPauseLock) {
while (mPaused) {
try {
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
}
/**
* Call this on pause.
*/
public void onPause() {
synchronized (mPauseLock) {
mPaused = true;
}
}
/**
* Call this on resume.
*/
public void onResume() {
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}
}