Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#安卓服务无错误崩溃_C#_Android_Xamarin_Xamarin.android - Fatal编程技术网

C#安卓服务无错误崩溃

C#安卓服务无错误崩溃,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,我正在用Xamarin制作一个Android应用程序。应用程序有一个在后台运行的TCP侦听器。因此,当应用程序未打开时,此侦听器仍处于活动状态。我已将其移动到作为服务运行。不幸的是,当启动时,UI会变得无响应,并且崩溃,而不会在调试器中抛出任何错误 在活动的新线程上启动服务: btnStart.Click += (object sender, EventArgs e) => { Thread listener = new Threa

我正在用Xamarin制作一个Android应用程序。应用程序有一个在后台运行的TCP侦听器。因此,当应用程序未打开时,此侦听器仍处于活动状态。我已将其移动到作为服务运行。不幸的是,当启动时,UI会变得无响应,并且崩溃,而不会在调试器中抛出任何错误

在活动的新线程上启动服务:

        btnStart.Click += (object sender, EventArgs e) => 
        {
            Thread listener = new Thread (() => 
            {
                StartService(new Intent(this, typeof(MyService)));
            });
            listener.Start();
        };
这是服务的代码:

    public override void OnStart(Android.Content.Intent intent, int startId)
    {
        base.OnStart (intent, startId);
        Log.Debug ("Service Class", "Service has started");
        DoActions ();
    }

    public void DoActions()
    {
        Log.Debug("Service Class", "getting local IP!");
        IPHostEntry host;
        string localIP = "";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork) 
            {
                localIP = ip.ToString ();
                break;
            }
        }

        int port = 1111;
        IPAddress localaddr = IPAddress.Parse (localIP);
        TcpListener tcpListener = new TcpListener (localaddr, port);
        Log.Debug ("Service Class", "accepted local IP, starting listener");
        tcpListener.Start ();
        Log.Debug ("Service Class", "You are now listening on " + localaddr + port);
        while (true)
        {
            Socket socket = tcpListener.AcceptSocket ();
            byte[] array = new byte[1024];
            socket.Receive (array);
            string @string = Encoding.UTF8.GetString (array);
            Log.Debug ("Service Class", "Message Received From Server:" + (@string.Replace ("\0", string.Empty)));
        }
    }
我看到了“You now Listing on”+localaddr+port”调试消息。在本例中,它是192.168.0.105端口1111。我有一个计时器每x秒写入一次调试,在应用程序崩溃之前,我会得到一些响应。当我使用同一活动作为任务运行它时,这个TCP侦听器工作正常

是否有一种方法可以将更多信息发送到调试器,以找出问题所在? 我正在使用4.2.2上的Tronsmart mk908b进行测试

提前谢谢

更新:
这似乎与Android服务生命周期有关。仍在寻找解决方法。

不要在线程内启动服务

    btnStart.Click += (object sender, EventArgs e) => 
    {
        StartService(new Intent(this, typeof(MyService)));
    };

请发布您的logcat.Info(364)/InputDispatcher:应用程序未响应:窗口{413e3dd0 u0 SecureX2.SecureX2/SecureX2.ListenActivity}。自事件发生以来,等待时间为5002.8毫秒,自等待开始以来,等待时间为5002.3毫秒。原因:等待时间是因为触摸窗口尚未完成对以前传递给它的输入事件的处理。在这5秒钟内,如果它收到消息,它将打印出来。谢谢!我知道,如果我这样做,它将在ui线程上运行。Testing Now相同结果:Info(364)/InputDispatcher:应用程序未响应:窗口{414d29e0 u0 SecureX2.SecureX2/SecureX2.ListenActivity}。自事件发生以来已为5003.0毫秒,自等待开始以来已为5002.8毫秒。原因:等待,因为触摸的窗口尚未完成对以前传递给它的输入事件的处理。@user1672867,您得到答案了吗?