Java Android Unity service NetworkMainThreadException

Java Android Unity service NetworkMainThreadException,java,android,sockets,unity3d,Java,Android,Sockets,Unity3d,我正在尝试使用安卓系统的服务将数据发送到Unity,我一直在遵循这一点,并且工作得非常完美,但是我需要创建一个套接字连接,从服务器接收数据,然后将其作为广播消息发送,就像教程所示。问题是,由于套接字连接,我在启动应用程序时遇到了networkMainThreadException。我知道使用AsyncTask可以解决这个问题,但我仍然不知道如何实现该服务并以这种方式发送广播消息 这是服务代码: private final Handler handler = new Handler(); //

我正在尝试使用安卓系统的服务将数据发送到Unity,我一直在遵循这一点,并且工作得非常完美,但是我需要创建一个套接字连接,从服务器接收数据,然后将其作为广播消息发送,就像教程所示。问题是,由于套接字连接,我在启动应用程序时遇到了
networkMainThreadException
。我知道使用
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服务和unity的第一步,但我还没有找到解决方案。 任何建议都是非常感谢的,或者如果您知道从android服务向unity发送数据的另一种方法,在该服务中进行套接字连接将非常好!
谢谢你的帮助

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

谢谢您的回答。对于IntentService更改,我仍然有相同的错误:
E/AndroidRuntime(15694):android.os.NetworkOnMainThreadException
。调用我正在做的服务
this.startService(newintent(this,LeapService.class))onCreate
功能中使用code>。知道为什么吗?删除处理程序并再次尝试,但如何执行可运行对象?现在我得到
E/AndroidRuntime(16680):java.lang.RuntimeException:无法启动服务com.test.leapservice。LeapService@41ef0dd8意图{cmp=com.test.leapservice/.leapservice}:android.os.NetworkOnMainThreadException
您认为套接字连接和发送广播调用正常吗?您是否从
服务
切换到
意向服务