Android 4.0.3上的TCP套接字

时间:2012-04-25 10:00:47

标签: java android

我正在开发一个项目,我通过Android与服务器建立TCP连接 我使用以下代码:

public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
Socket s;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.braincandy);
    player.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
    try {
        s.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    player.start();
    try {
        s = new Socket("192.168.1.54", 64000);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 }

我正在运行连接作为服务。

真正的问题是我可以与我的Android手机(2.3.7)和模拟器(2.3.3)建立连接但是当我想在我的平板电脑(4.0.3)上测试时,我的应用程序总是崩溃当我想开始连接时。

有人可以帮我这个吗?

这是logcat日志:     致命异议:主要     java.lang.RuntimeException:无法启动服务

com.gunther.servicetcp.MyService@412b0a98 with Intent 
{    cmp=com.gunther.servicetcp/.MyService }: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376)
at android.app.ActivityThread.access$1900(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
0at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:566)
at java.net.Socket.tryAllAddresses(Socket.java:127)
at java.net.Socket.<init>(Socket.java:177)
at java.net.Socket.<init>(Socket.java:149)
at com.gunther.servicetcp.MyService.onStart(MyService.java:53)
at android.app.Service.onStartCommand(Service.java:438)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
:   ... 10 more

2 个答案:

答案 0 :(得分:4)

您不应该在主线程上执行网络操作。这会导致您的应用无响应。在Honeycomb之前你可以侥幸成功,但是Honeycomb和更新的Android版本会检查并抛出你得到的异常。另请参阅this page of the Android Developers API

答案 1 :(得分:2)

Services的{​​{1}}方法在主线程上运行,(是的,服务似乎在应用程序的主线程上运行。)所以你应该在{{1}中分叉另一个线程方法并在该线程中完成您需要做的所有事情。