简体   繁体   English

DatagramSocket正在接收时,Android onTouchEvent无法正常工作

[英]Android onTouchEvent won't work while DatagramSocket is receiving

I'm trying to allow my user to move a block around a screen while also listening for connected on a DatagramSocket, here is my onTouchEvent 我试图允许我的用户在屏幕上移动块,同时还监听DatagramSocket上的连接,这是我的onTouchEvent

public boolean onTouchEvent( MotionEvent event )
{
    if ( event.getAction() == MotionEvent.ACTION_DOWN )
    {
        if( event.getY() > getHeight() - 50 )
        {
            gameState.setRunning( false );
            ((Activity)getContext()).finish();
        }
        else
        {
            touched = true;
            Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
        }
        checkCollisions( event.getX(), event.getY() );
    }

    if ( event.getAction() == MotionEvent.ACTION_MOVE )
    {
        checkCollisions( event.getX(), event.getY() );
    }

    if ( event.getAction() == MotionEvent.ACTION_UP ) 
    {
        touched = false;
    }
    return true;

}

And here is my server code 这是我的服务器代码

public void run()
{
    Log.d(TAG, "Ip address used:" + getLocalIPAddress() );
    while( true )
    {
        //Receive some data
        byte[] buf = new byte[256];
        DatagramPacket packet = new DatagramPacket( buf, buf.length );
        try 
        {
            socket.receive( packet );
        } 
        catch (IOException e) 
        {
            Log.d(TAG, "Error with receiving data");
            e.printStackTrace();
        }

        String data = new String( buf, 0, packet.getLength() );

        Log.d(TAG, "Data received was :" + data);

    }
}

Now my server works fine, but while the server is waiting for a connection, the client can't do anything, now I know a thread could be started, but won't that still block the onTouchEvent? 现在我的服务器工作正常,但是在服务器等待连接时,客户端无法执行任何操作,现在我知道可以启动线程了,但是那还会阻止onTouchEvent吗?

Canvas 帆布

Update 更新资料

I implemented the run as a doInBackground using AsyncTask, it now works fine now without a problem, i can play with my application and also wait for clients to connect, this should work fine, Cheers for the help people. 我使用AsyncTask将其实现为doInBackground,现在可以正常运行了,而且没有问题,我可以玩我的应用程序,也可以等待客户端连接,这应该很好,为帮助人员加油打气。

Canvas 帆布

In Android, every View related aspects should be handled in the main thread, that is, the UI thread. 在Android中,每个与View相关的方面都应在主线程(即UI线程)中处理。 Time consuming, heavy calculations or simple blocking tasks should remain in an another thread to avoid the user interface to be frozen. 费时,繁重的计算或简单的阻止任务应保留在另一个线程中,以避免冻结用户界面。

Taking care of keeping your user interface fluid and responsive is important. 保持用户界面流畅和响应很重要,这一点很重要。 If you don't, the ActivityManager will certainly end up popping up an alert box asking the user if he would like to close you application as it is not responding properly to his interactions. 如果您不这样做, ActivityManager肯定会最终弹出一个警告框,询问用户是否要关闭您的应用程序,因为该应用程序无法正确响应其交互。 This called an ANR (Application not responding). 这称为ANR (应用程序无响应)。

You might want to read this very good article talking about the responsiveness of every application should guarantee. 您可能想阅读这篇很好的文章,其中谈到了每个应用程序应确保的响应能力。 It is written by the Google Android team and contains a lot of precious advises. 它由Google Android团队编写,包含许多宝贵的建议。

I need a bit more space than a comment ... 我需要的空间比评论多一点...

Key point: creating classes doesn't create threads, unless they are thread extending classes ( AsyncTask , Thread , etc). 重点:创建类不会创建线程,除非它们是线程扩展类( AsyncTaskThread等)。

Typically for your application the server would run in one thread, the game logic in another, the image updater in a third and the UI thread would refresh the display from this third thread when told that there was a new thing to display. 通常,对于您的应用程序,服务器将在一个线程中运行,游戏逻辑在另一个线程中运行,图像更新程序在第三线程中运行,UI线程将在得知有新内容要显示时从第三线程刷新显示。

An example of a similar approach is given in http://www.javacodegeeks.com/2011/07/android-game-development-basic-game_05.html . http://www.javacodegeeks.com/2011/07/android-game-development-basic-game_05.html中提供了类似方法的示例。 I would suggest using that as a starting point and adding in an extra Server thread on a similar basis, which should get things going. 我建议以此为起点,并在类似的基础上添加一个额外的Server线程,这应该可以解决问题。

Sorry if I'm telling you stuff you already know, but better safe than sorry. 抱歉,如果我告诉您您已经知道的内容,但是比后悔更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM