Android正在使用api 10而不是16

时间:2014-01-07 17:32:48

标签: java android android-emulator

UDP实施:

@Override
protected Void doInBackground(Void... params) {
    try {

        this.clientSocket = new DatagramSocket();
            this.IPAddress = InetAddress.getByName("10.0.0.2");
            this.sendData = new byte[1024];

    }
    catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

        public void mouse(int x, int y) {

            try {

                  String sentence = "{" + x + "," + y + "}";
                  this.sendData = sentence.getBytes();
                  DatagramPacket sendPacket = new DatagramPacket(this.sendData, this.sendData.length, this.IPAddress, 9871);
                  this.clientSocket.send(sendPacket);

            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.v("tests", "test");
    if(event.getAction() == MotionEvent.ACTION_DOWN ){
        oldX = (int) event.getX();
        oldY = (int) event.getY();
    }
    else if(event.getAction() == MotionEvent.ACTION_MOVE ){
        newX = (int) event.getX();
        newY = (int) event.getY();

        xDiff = newX - oldX;
        yDiff = newY - oldY;
        Log.v("xdiff", ""+xDiff);
        Log.v("ydiff", ""+yDiff);
        oldX = newX;
        oldY = newY;
        a.mouse(xDiff, yDiff);
    }
    else if(event.getAction() == MotionEvent.ACTION_UP){

    }
    return true;
}

python udp serv:

import socket

PORT = 9871
IP = socket.gethostbyname(socket.gethostname())

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((IP, PORT))
print "Server %s" % IP

while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        if data:
            print "received message:", data
        else:
            print "dead"

logcat的:

01-07 17:18:51.509: E/InputEventReceiver(583): Exception dispatching input event.
01-07 17:18:51.517: D/AndroidRuntime(583): Shutting down VM
01-07 17:18:51.517: W/dalvikvm(583): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-07 17:18:51.567: E/AndroidRuntime(583): FATAL EXCEPTION: main
01-07 17:18:51.567: E/AndroidRuntime(583): android.os.NetworkOnMainThreadException
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-07 17:18:51.567: E/AndroidRuntime(583):  at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
01-07 17:18:51.567: E/AndroidRuntime(583):  at libcore.io.IoBridge.sendto(IoBridge.java:473)
01-07 17:18:51.567: E/AndroidRuntime(583):  at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
01-07 17:18:51.567: E/AndroidRuntime(583):  at java.net.DatagramSocket.send(DatagramSocket.java:284)
01-07 17:18:51.567: E/AndroidRuntime(583):  at com.example.sockettest1.WriteToSocket.mouse(WriteToSocket.java:82)
01-07 17:18:51.567: E/AndroidRuntime(583):  at com.example.sockettest1.Mouse_test1.onTouchEvent(Mouse_test1.java:46)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
01-07 17:18:51.567: E/AndroidRuntime(583):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.View.dispatchPointerEvent(View.java:7307)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4205)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4245)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.Choreographer.doFrame(Choreographer.java:523)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.os.Handler.handleCallback(Handler.java:615)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.os.Looper.loop(Looper.java:137)
01-07 17:18:51.567: E/AndroidRuntime(583):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-07 17:18:51.567: E/AndroidRuntime(583):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 17:18:51.567: E/AndroidRuntime(583):  at java.lang.reflect.Method.invoke(Method.java:511)
01-07 17:18:51.567: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-07 17:18:51.567: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-07 17:18:51.567: E/AndroidRuntime(583):  at dalvik.system.NativeStart.main(Native Method)
01-07 17:18:51.637: W/ActivityManager(162):   Force finishing activity com.example.sockettest1/.Mouse_test1
01-07 17:18:51.647: W/WindowManager(162): Failure taking screenshot for (246x410) to layer 21010
01-07 17:18:51.947: I/Choreographer(162): Skipped 43 frames!  The application may be doing too much work on its main thread.
01-07 17:18:52.205: W/ActivityManager(162): Activity pause timeout for ActivityRecord{41506518 com.example.sockettest1/.Mouse_test1}
01-07 17:18:52.557: I/Choreographer(261): Skipped 41 frames!  The application may be doing too much work on its main thread.
01-07 17:18:57.049: I/InputDispatcher(162): Application is not responding: Window{4158f1a0 com.example.sockettest1/com.example.sockettest1.Mouse_test1 paused=false}.  It has been 5007.4ms since event, 5006.7ms since wait started.  Reason: Waiting because the touched window has not finished processing the input events that were previously delivered to it.
01-07 17:18:57.049: I/WindowManager(162): Input event dispatching timed out sending to com.example.sockettest1/com.example.sockettest1.Mouse_test1

现在这个代码在ADT仿真器上的API10上运行得非常好,但它在运行4.1.2(API16)的设备(SGS2)上崩溃所以我在API16上创建了一个新的仿真器,结果与结果相同我的设备所以我认为它与API兼容性有关。 它在启动时崩溃(因为我可能执行asynctask oncreate)

1 个答案:

答案 0 :(得分:1)

如日志所述,您正在执行NetworkOnMainThread。您的mouse()方法在UI线程上运行,因此其中的网络相关代码需要在后台线程上运行。

编辑:

我不确定你是如何更新你的代码的,但我会尝试运行'a.mouse(xDiff,yDiff);'使用后台线程(您也可以使用AsyncTask)。您可以在Activity中创建一个运行mouse()方法的Runnable类:

public class updateThread implements Runnable
{
    private int x;
    private int y;

    public updateThread(int x, int y)
    {
        this.x = x
        this.y = y
    }

    public void run()
    {
        a.mouse(x, y);
    }
}

然后在onTouchEvent中,而不是调用mouse(),创建一个实例并启动它:

Thread thread = new Thread(new updateThread(xDiff, yDiff));
thread.start();