简体   繁体   English

适用于Android的Mono-致电服务后活动崩溃

[英]Mono for Android - Activity crash upon service call

My application has a UI (implemented with an Activity ) and a service (implemented with the IntentService ). 我的应用程序具有一个UI(由Activity实施)和一个服务(由IntentService实施)。 The service is used to send data (synchronous, using NetworkStream.Write ) to a remote server as well as to update the transmission status to the UI (implemented using Broadcast Receiver method). 该服务用于将数据(使用NetworkStream.Write同步)发送到远程服务器,以及更新UI的传输状态(使用Broadcast Receiver方法实现)。

Here is my problem: 这是我的问题:

  • The application works properly if the size of the buffer used for the NetworkStream.Write is 11 KB or less. 如果用于NetworkStream.Write的缓冲区大小为11 KB或更小,则应用程序将正常运行。

  • However, if the size of the buffer is larger than 11 KB, say 20 KB (this size needed in order to send jpg images), then the sevice keeps working properly (verified with log file), nonetheless the UI its gone (similar as if device's back button is pushed) and I can't find the way to bring it back. 但是,如果缓冲区的大小大于11 KB,例如20 KB(此大小需要发送jpg图像),则服务将继续正常工作(已通过日志文件验证),但是UI消失了(类似于(如果按下了设备的后退按钮),我找不到将其恢复的方法。 Its important to point out that in this case the Activity its not going into OnStop() nor OnDestroy() states. 重要的是要指出,在这种情况下,Activity不会进入OnStop()OnDestroy()状态。

  • At first I thought this would be some ApplicationNotResponding related issue due to a server delay, yet the UI crashes after about 5 sec. 起初,我认为这是由于服务器延迟而引起的一些与ApplicationNotResponding相关的问题,但是UI大约在5秒钟后崩溃了。

  • Moreover, this only happens with the Hardware version. 此外,这仅在硬件版本中发生。 The emulator version works fine. 模拟器版本工作正常。

     // SEND STREAM: Byte[] outStream = new Byte[20000]; // -- Set up TCP connection: -- TcpClient ClientSock = new TcpClient(); ClientSock.Connect("myserver.com", 5555); NetworkStream serverStream = ClientSock.GetStream(); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); // . . . // RECEIVE STREAM: inStream.Initialize(); // Clears any previous value. int nBytesRead = 0; nBytesRead = serverStream.Read(inStream, 0, 1024); // -- Closing communications socket: -- ClientSock.Close(); 

Well you may want to change your application to use asynctask and take a look to the guide : http://developer.android.com/training/basics/network-ops/connecting.html 好吧,您可能需要更改您的应用程序以使用asynctask并查看该指南: http : //developer.android.com/training/basics/network-ops/connecting.html

Network operations can involve unpredictable delays. 网络操作可能会涉及不可预测的延迟。 To prevent this from causing a poor user experience, always perform network operations on a separate thread from the UI. 为防止这种情况导致不良的用户体验,请始终在与UI分开的线程上执行网络操作。

Since android 4.0 it's impossible to perform network related task in the same thread as the UI thread. 从android 4.0开始,就不可能在与UI线程相同的线程中执行与网络相关的任务。 Also just to be clear http://developer.android.com/guide/components/services.html 同样也要明确http://developer.android.com/guide/components/services.html

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process 警告:服务在其宿主进程的主线程中运行-该服务不会创建自己的线程,也不会在单独的进程中运行

One thing first: I would have been commented the question to clarify one thing before I give an answer, but unfortunately I don't have enough reputation yet. 首先要解决的一件事:在给出答案之前,我会先被评论这个问题以澄清一件事,但是不幸的是,我还没有足够的声誉。

The thing I would have asked for is: Why do you need to have a buffer greater than 11k to send an JPG image? 我要问的是:为什么需要一个大于11k的缓冲区才能发送JPG图像?

I nearly do the same in one (async) task with an image of 260k, but with a buffer of 10240 Bytes. 我几乎在一个(异步)任务中以260k的映像执行了相同的操作,但是缓冲区为10240 Bytes。 Works without difficulties. 工作没有困难。

byte[] buffer = new byte[10240];
for (int length = 0; (length = in.read(buffer)) > 0;) {
  outputStream.write(buffer, 0, length);
  outputStream.flush();
  bytesWritten += length;
  progress = (int) ((double) bytesWritten * 100 / totalBytes);
  publishProgress();
}
outputStream.flush(); 

I use this part to read an JPG image from resources or SD and post to my server. 我使用这一部分从资源或SD中读取JPG图像并将其发布到我的服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM