English 中文(简体)
一段时间后,机器人在TCP套接字连接故障
原标题:Android long-during TCP socket connection failure after some time

i 写入客户端- 服务器应用程序, 它使用 TCP 套接字连接。 在我和机器人项目中, Service 为收听 TCP 套接字创建一条线 。

似乎一切都很好。但我有一个问题。。 我的网络服务在背景上运行很好; 但过了一段时间( 10-15 分钟), 当我试图再次打开应用程序( 主要活动)时, 我无法从插座连接中得到回应。 它冻结了还是什么??? 我无法发送或从插座获得 TCP 信息。 原因是什么? 我正在通过 3G 连接在手机上工作 。

(此外,模拟器中运行的应用程序没有遇到这样的问题; 我猜它的连接是稳定的, 长期的 )

谢谢你的回答

问题回答

由于电力优化,也许连通性(GPRS/HSDPA/Wifi)发生变化,你的连通极有可能被删除。

为了维持连接, 您的背景服务需要使用 PowerManager 类来申请锁住 。 这样可以防止设备进入节电模式, 从而切断您的插座 。 但请注意, 这会大大降低设备的电池使用寿命 。

另外,您还需要处理连接的变更,这些变更打破了您的开放式连接。Android发送了一个名为 android.net.conn.conn.concONNECTIVITY_Change 的广播信息,通知连接的变更。

仅当设备正在积极使用或只是使用 C2DM 推进通知时才建立连接

当我在应用程序中体验到这样的事情时,通常是因为手机上的电源优化(不能被覆盖 ) 。 如果插座闲置时间太长, 它会自动关闭, 需要重新开放 。

您不时发送数据吗? 比如执行心跳协议? 如果您不是, 您应该... 或者它可能与插座 READ/ WriteTimeOUT 有关 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签