【问题标题】:Android-Unity service NetworkOnMainThreadExceptionAndroid-Unity 服务 NetworkOnMainThreadException
【发布时间】:2014-03-28 22:06:14
【问题描述】:

我正在尝试使用 Android 的服务向 Unity 发送数据,我一直在关注 tutorial 这样做并且效果很好,但是我需要创建一个套接字连接来接收来自服务器的数据和然后就像教程显示的那样将其作为广播消息发送。问题是由于套接字连接,我在启动应用程序时收到了NetworkOnMainThreadException。我已经看到使用AsyncTask 可以解决问题,但我仍然不知道如何实现服务并以这种方式发送广播消息。

这是服务代码:

private final Handler handler = new Handler();


// It's the code we want our Handler to execute to send data
private Runnable sendData = new Runnable() {
        // the specific method which will be executed by the handler
        public void run() {

                Socket socket = null;

                try{

                    socket = new Socket("x.x.x.x", 1337);

                    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                    String line ="";

                    line = input.readLine();                    

                    numIntent++;

                    // sendIntent is the object that will be broadcast outside our app
                    Intent sendIntent = new Intent();

                    // We add flags for example to work from background                    
                    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION|Intent.FLAG_FROM_BACKGROUND|Intent.FLAG_INCLUDE_STOPPED_PACKAGES  );

                    // SetAction uses a string which is an important name as it identifies the sender of the itent and that we will give to the receiver to know what to listen.
                    // By convention, it's suggested to use the current package name
                    sendIntent.setAction("com.test.service.IntentToUnity");

                    // Here we fill the Intent with our data, here just a string with an incremented number in it.
                    //sendIntent.putExtra(Intent.EXTRA_TEXT, "Intent "+numIntent);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, line);
                    // And here it goes ! our message is send to any other app that want to listen to it.
                    sendBroadcast(sendIntent);

                    // In our case we run this method each second with postDelayed
                    handler.removeCallbacks(this);
                    handler.postDelayed(this, 1000);

                }catch(IOException e){
                    e.printStackTrace();

                }
        }
};

// When service is started
@Override
public void onStart(Intent intent, int startid) {
        // We first start the Handler
        handler.removeCallbacks(sendData);
        handler.postDelayed(sendData, 1000);
}

我不确定这是否是一个简单的问题,因为这是我使用 android 服务和统一的第一步,但我还没有找到解决方案。 非常感谢任何建议,或者如果您知道另一种从建立套接字连接的 android 服务向统一发送数据的方法,那就太好了! 感谢您的帮助!

【问题讨论】:

    标签: java android sockets unity3d


    【解决方案1】:

    Service 切换到IntentService,应该可以解决问题。 IntentService 运行在单独的线程上,而Service 运行在 UI 线程上

    【讨论】:

    • 感谢您的回答。 IntentService 更改时我仍然遇到同样的错误:E/AndroidRuntime(15694): android.os.NetworkOnMainThreadException。要调用服务,我只在onCreate 函数上使用this.startService(new Intent(this, LeapService.class));。知道为什么吗?
    • 移除 Handler 再试一次
    • 但是如何执行 Runnable 对象呢?
    • 现在我得到E/AndroidRuntime(16680): java.lang.RuntimeException: Unable to start service com.test.leapservice.LeapService@41ef0dd8 with Intent { cmp=com.test.leapservice/.LeapService }: android.os.NetworkOnMainThreadException 你认为套接字连接和sendbroadcast 调用正常吗?
    • 你从Service切换到IntentService了吗?
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2012-11-16
    • 2015-01-01
    • 2021-03-21
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多