-1
UDP實現:Android平臺上的API 10而不是16
@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;
}
蟒蛇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
下面這段代碼工作完全正常的API10上ADT仿真器,但它在運行4.1.2(API16)的設備(SGS2)上崩潰,所以我在API16上創建了一個新仿真器,結果是sam e作爲結果在我的設備上,所以我認爲它與API兼容性有關。 它在發射時崩潰(因爲可能我執行asynctask oncreate)
我已經將數據傳輸移到AsyncTask,但現在它發送數據但在一兩秒後崩潰,因爲當我a.execute()它仍在運行時01-07 17:48:35.437:E/AndroidRuntime(666): java.lang.IllegalStateException:無法執行任務:任務已在運行。我應該如何改變代碼,以便在Asynctask線程上工作而不會崩潰? – Shay
請發佈您的更新代碼。 –