我正在创建一个需要线程的应用程序。当我测试生命周期时,我得到了一个奇怪的结果。当我从eclipse运行应用程序时,所有内容都被正确调用 - onCreate()
然后onResume()
。当我按下手机上的主页按钮时,onPause()
被呼叫。但是,当我从手机onResume()
重新启动它时,从未调用过。这是否意味着我的线程没有创建并重新启动?这是我的课程:
public class RenderView extends SurfaceView implements Runnable {
Thread thread = null;
SurfaceHolder holder;
boolean running = false;
Bitmap frameBuffer;
public RenderView(Context context, Bitmap frameBuffer) {
super(context);
holder = getHolder();
this.frameBuffer = frameBuffer;
}
@Override
public void run() {
while(running) {
if(!holder.getSurface().isValid()) {
continue;
}
Canvas canvas = holder.lockCanvas();
canvas.drawARGB(255, 150, 150, 10);
holder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
running = false;
while(true) {
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public void resume() {
running = true;
thread = new Thread(this);
thread.start();
}
}
public class GameHandler extends Activity {
RenderView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Game Handler", "onCreate called");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
boolean isLandscape = this.getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE;
int frameBufferWidth = isLandscape ? 1280 : 800;
int frameBufferHeight = isLandscape ? 800: 1280;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Bitmap.Config.RGB_565);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float scaleX = (float) frameBufferWidth / metrics.widthPixels;
float scaleY = (float) frameBufferHeight / metrics.heightPixels;
view = new RenderView(this, frameBuffer);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onResume() {
super.onResume();
Log.d("GameHandler ", "OnResume Called");
view.resume();
}
@Override
public void onPause() {
super.onPause();
Log.d("GameHandler ", "onPaused Called");
view.pause();
}
}