我已经尝试过一些方法来做到这一点,并没有停止。 我有启动3线程的MainActivity。我想在用户按下“返回”底部时或由于某种原因应用程序停止(例如电话呼叫)时停止线程。 并且在活动再次出现后(当用户回到应用程序时),线程将从他们停止的地方继续。 MainActivity中定义的所有线程以及它们在那里启动。 谢谢!如何使用线程暂停活动并在之后恢复 - android
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();
}
}
}
你有没有检查过? http://stackoverflow.com/questions/6776327/how-to-pause-resume-thread-in-android – Rahul
是的。也许我在onCreate中做错了什么?我将用代码编辑我的问题。 – user1932595
当您的活动暂停时,您是否在所有线程上调用了'onResume'方法?在发布的代码中没有看到该实现。如果您的支持Back按钮(从栈中弹出活动)请记住您的活动已被破坏,因此您必须将线程的状态保存在数据库或其他地方,以便稍后可以恢复它们。 –