如何在Andorid的另一个类的返回值进行连续检查? [英] How do I perform a continuous check on andorid of the returned value of another class?
问题描述
我想在Android的线程进行检查,以检查另一个类的方法的返回值。这里是我的code迄今:
I want to perform a check in an android thread to check the returned value of a method in another class. Here is my code so far:
public class HelloWorldAndroid extends AndroidApplication {
private MyGame myGame;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**********Other lines Omitted***********
myGame = new MyGame(false, 30, new AndroidLeaderboard());
//check to see if the game is over
Thread background = new Thread(new Runnable() {
@Override
public void run() {
if (myGame.gameOver()) {
//if the game is over go to another screen
startActivity(new Intent(HelloWorldAndroid.this, MainActivity.class));
}
}
});
background.start();
}
香港专业教育学院试图用一个处理器来实现这一点,但它只是不执行检查,所以当值为true它仍然运行。有谁知道我怎么能执行检查 myGame.gameOver()真
持续整个线程的运行=(这样我就可以移动到不同的活动)。
Ive tried to implement this with a handler but it just doesn't perform the check so when the value is true it still runs. Does anyone know how I can perform the check myGame.gameOver() = true
continuously throughout the running of this thread (so I can move on to a different activity).
一直绞尽脑汁几天就这一个,仍然什么都没有:•
任何想法都欢迎。
Been racking my brains for days on this one and still have nothing :S Any ideas are welcome.
感谢
推荐答案
为什么不创建某种监听器(接口)?的
Why not create some kind of Listener (interface) ?
小而基本的例子,
myGame = new MyGame(false, 30, new AndroidLeaderboard());
myGame.setGameOverListener(this);
注意:您不需要 setGameOverListener
方法,你也可以改变你的构造函数有一个监听器参数
Note: You don't need a setGameOverListener
method, you could also change your constructor to have a listener argument.
监听器是这样的:
interface GameOverListener {
abstract public void notifyGameOver();
}
和创建侧的方法,你MyGame对象:
And create a method in side you MyGame object:
setGameOverListener(GameOverListener gol){
this.gol = gol;
}
和您的活动将实施监听器,并在 notifyGameOver()
方法,你能打开活动。
And your Activity would implement that listener, and in the notifyGameOver()
method you would open the activity.
这样的:
public void notifyGameOver(){
startActivity(new Intent(HelloWorldAndroid.this, MainActivity.class));
}
要通知你的游戏已经结束只是让你MyGame对象调用 notifyGameOver()
方法:
To notify that your game is over just let your MyGame object call the notifyGameOver()
method:
gol.notifyGameOver();
这篇关于如何在Andorid的另一个类的返回值进行连续检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!