重启后应用程序丢失触摸输入

时间:2012-08-08 23:02:37

标签: java android

我正在Android上编写2D引擎。我可以根据触摸事件接收触摸输入并更新我的场景但是有问题。失去焦点后重新启动应用程序(单击主屏幕按钮,接收呼叫等)。我无法再接收触摸输入,屏幕仍显示内容但仍保持冻结状态。 有没有办法解决这个问题?

以下是代码:

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.media.AudioManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.DisplayMetrics;

public class SolarFighter extends Activity implements OnTouchListener
{
Game            game;
GraphicsManager graphicsMan;
SoundManager    soundMan;
ErrorManager    errorMan;
FrameSetManager frameSetMan;
SpriteManager   spriteMan;
InputManager    inputMan;
WakeLock        wakeLock;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //
    //Go full screen
    //

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wakeLock                  = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");


    //
    // Get system information
    //

    DisplayMetrics displaymetrics = new DisplayMetrics();

    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    int height = displaymetrics.heightPixels;
    int width  = displaymetrics.widthPixels;

    //
    // Create Graphics Manager and load spriteSheets load fonts
    //

    graphicsMan = new GraphicsManager(width,height,this);
    graphicsMan.addFont(0, "fonts/font.ttf", this);      

    //
    // Create Sound Manager and add sounds
    //

    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    //
    // Create Input Manager
    //
    inputMan = new InputManager();

    //
    // Create Frame Set Manager and add frame sets
    //

    //
    // Create Sprite Manager and add sprites
    //

    //
    // Create Entity Manager and add entities
    //

    //
    // Create Game
    //

    game = new Game(this);

    game.setOnTouchListener(this);  
    setContentView(game);

}

public void onResume() 
{
    super.onResume();

    wakeLock.acquire();

    game.resume();
}

public void onPause()
{
    super.onPause();

    wakeLock.release();

    game.pause();
}

public boolean onTouch(View v, MotionEvent event) 
{
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            inputMan.setTouchState(InputManager.TOUCH_DOWN);
            inputMan.setLastX((int)(event.getX()));
            inputMan.setLastY((int)(event.getY()));
        break;

        case MotionEvent.ACTION_MOVE:
            inputMan.setTouchState(InputManager.TOUCH_MOVE);    
            inputMan.setCurrentX((int)(event.getX()));
            inputMan.setCurrentY((int)(event.getY()));
            inputMan.setRelativeX(inputMan.getCurrentX() - inputMan.getLastX());
            inputMan.setRelativeY(inputMan.getCurrentY() - inputMan.getLastY());    
            inputMan.setLastX(inputMan.getCurrentX());
            inputMan.setLastY(inputMan.getCurrentY());
        break;

        case MotionEvent.ACTION_CANCEL:
            inputMan.setTouchState(InputManager.TOUCH_UP);
        break;

        case MotionEvent.ACTION_UP:
            inputMan.setTouchState(InputManager.TOUCH_UP);
        break;
    }
    return true;
}

class Game extends SurfaceView implements Runnable
{
    private Thread            renderThread = null;
    private SurfaceHolder     holder;
    private volatile boolean  running = false;  

    private int  x  = 0,y = 0;
    private int  xx = 250;
    private int  xy = 400;

    public Game(Context context)
    {
        super(context);

        holder = getHolder();   
    }

    public void resume() 
    {
        running      = true;
        renderThread = new Thread(this);

        renderThread.start();
    }

    public void run() 
    {
        while(running) 
        {
            if(!holder.getSurface().isValid())
            {
                continue;
            }

            //
            // Update Scene
            //


            //
            // Render scene
            //

            graphicsMan.beginScene(holder);
            graphicsMan.clearScene();   
            x = inputMan.getRelativeX();
            y = inputMan.getRelativeY();
            if(inputMan.getTouchState() == InputManager.TOUCH_MOVE)
            {
                xx += x;
                xy += y;
            }
            graphicsMan.drawText(0, "X", Color.RED,40,xx, xy);
            graphicsMan.endScene(holder);
        }
    }

    public void pause() 
    {
        running = false;

        while(true) 
        {
            try 
            {
                renderThread.join();
            } 
            catch (InterruptedException e)
            {
                // retry
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

我摆脱了Game暂停方法的整个声明,问题解决了。